3

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;
    }
}
5
  • By the way, I'm curious, can you post the code of your method? Many years ago I developed one with the same purpose, but I did it like "replace two consecutive spaces into one space while the string contains two consecutive spaces", which I think it is not the most efficient way. Commented Oct 28, 2009 at 12:04
  • You got it. Code posted in Edit. Commented Oct 28, 2009 at 12:18
  • 2
    I have the feeling that this might be a nice scenario to use Regex.Replace. Find any groups of consecutive spaces with length > 1 and replace it with a single one. Of course you can start by trimming the input, which will get rid of any whitespace pre- or suffixes. Commented Oct 28, 2009 at 12:46
  • I absolutely have regex implementation as well. This code was written as part of a challenge to solve this recursively. Commented Oct 28, 2009 at 13:08
  • @curious_geek I think this stack-exchange proposal might be of interest to you. If it is show your support and help get it into beta! :) Commented Jan 16, 2011 at 23:54

9 Answers 9

14

What about CollapseSpaces?

Sign up to request clarification or add additional context in comments.

6 Comments

In .Net it's already a convention that Trim refers to removing whitespace so that's maybe an argument in favour of Trim[xxx]()?
Actually I take that back, since Trim removes the whitespace, this requirement is to reduce the count to 1
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.
'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.
Thank you. This is my most successful answer in SO so far: accepted and the third most voted in just 30 minutes! :-)
|
7

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.

2 Comments

Collapse what? it seems to vague to me, the developer ideally shouldn't need to read the documentation to find out...
Exactly - I want the developer to intuitively relate the method name with the functionality. Collapse may work just like Trim.
6

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);
    }
}

Comments

2

In ruby I believe they call this squeeze

Comments

2

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.

Comments

1

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(' '));

Comments

0

CollapseExtraWhitespace

Comments

0

PaulaIsBrilliant of course!

Comments

0

How is makeCompact?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.