views:

219

answers:

9

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;
    }
}
+14  A: 

What about CollapseSpaces?

Konamiman
In .Net it's already a convention that Trim refers to removing whitespace so that's maybe an argument in favour of Trim[xxx]()?
rohancragg
Actually I take that back, since Trim removes the whitespace, this requirement is to reduce the count to 1
rohancragg
When I see Trim, I automatically think on removing something from the start or the end, not inbetween. Collapse sounds to me like "make 1 out of N". Anyway I guess it is a matter of taste, and you may be right.
Konamiman
'Collapse' is small, intuitive, relative and also has better recall factor for front-end developers. I'll go with 'collapse' to also include the intuitive reference for overloads. I'm accepting your answer as vote of thanks to originate the idea of 'collapse'. I'll work on my english vocabulary to come up with such namings my own. :) Thanks.
this. __curious_geek
Thank you. This is my most successful answer in SO so far: accepted and the third most voted in just 30 minutes! :-)
Konamiman
Congratulations. You have the code as well. Check it out and let me know your comments on the efficiency and accuracy of the code.
this. __curious_geek
+7  A: 

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.

Jon Skeet
How about just 'Collapse'
this. __curious_geek
Collapse what? it seems to vague to me, the developer ideally shouldn't need to read the documentation to find out...
rohancragg
Exactly - I want the developer to intuitively relate the method name with the functionality. Collapse may work just like Trim.
this. __curious_geek
+2  A: 

In ruby I believe they call this squeeze

Dave
A: 

CollapseExtraWhitespace

zvolkov
+2  A: 

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.

Rob van Groenewoud
+6  A: 

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);
    }
}
LukeH
+1  A: 

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(' '));
Rob van Groenewoud
A: 

PaulaIsBrilliant of course!

Florian Doyon
A: 

How is makeCompact?

fastcodejava