Question

I have written an extension method for string manipulation. I'm confused what should I name it - since this will become part of the base library front-end developers in the team will use. Here's the profile of the class member.

Info: Utility Extension method for String types. Overloads of this method may do the same thing characters other than space [with what supplied in argument]
Purpose: Trims down all intermediate or in-between spaces to single space.
Ex:

string Input = "Hello      Token1    Token2     Token3    World!  ";
string Output = Input.TrimSpacesInBetween();
//Output will be: "Hello Token1 Token2 Token3 World!"

I have read [in fact I'm reading] the Framework Design guidelines but this seems to be bothering me.

Some options I think..

TrimIntermediate();  
TrimInbetween();

Here's the code on Request:

It's recursive..

public static class StringExtensions
{
    public static string Collapse(this string str)
    {
        return str.Collapse(' ');
    }

    public static string Collapse(this string str, char delimeter)
    {
        char[] delimeterts = new char[1];
        delimeterts[0] = delimeter;
        str = str.Trim(delimeterts);

        int indexOfFirstDelimeter = str.IndexOf(delimeter);
        int indexTracker = indexOfFirstDelimeter + 1;

        while (str[indexTracker] == delimeter)
            indexTracker++;

        str = str.Remove(indexOfFirstDelimeter + 1, indexTracker - indexOfFirstDelimeter - 1);
        string prevStr = str.Substring(0, indexOfFirstDelimeter + 1);
        string nextPart = str.Substring(indexOfFirstDelimeter + 1);

        if (indexOfFirstDelimeter != -1)
            nextPart = str.Substring(indexOfFirstDelimeter + 1).Collapse(delimeter);

        string retStr = prevStr + nextPart;

        return retStr;
    }
}
Was it helpful?

Solution

What about CollapseSpaces?

OTHER TIPS

CollapseSpaces is good for just spaces, but to allow for the overloads you might want CollapseDelimiters or CollapseWhitespace if it's really just going to be for various whitespace characters.

Not really an answer, more a comment on your posted code...

You could make the method a lot shorter and more understandable by using a regular expression. (My guess is that it would probably perform better than the recursive string manipulations too, but you would need to benchmark to find out for sure.)

public static class StringExtensions
{
    public static string Collapse(this string str)
    {
        return str.Collapse(' ');
    }

    public static string Collapse(this string str, char delimiter)
    {
        str = str.Trim(delimiter);

        string delim = delimiter.ToString();
        return Regex.Replace(str, Regex.Escape(delim) + "{2,}", delim);
    }
}

In ruby I believe they call this squeeze

NormalizeWhitespace ? This way is more clear that there will be a usable value left after processing. As other have stated earlier, 'Collapse' sounds somewhat rigorous and might even mean that it can return an empty string.

Try this, it works for me and seems to be a lot less complicated than a recursive solution...

public static class StringExtensions
{
    public static string NormalizeWhitespace(this string input, char delim)
    {
        return System.Text.RegularExpressions.Regex.Replace(input.Trim(delim), "["+delim+"]{2,}", delim.ToString());
    }
}

It can be called as such:

Console.WriteLine(input.NormalizeWhitespace(' '));

CollapseExtraWhitespace

PaulaIsBrilliant of course!

How is makeCompact?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top