What should I call this extension method?

I wrote an extension method for string manipulations. I am confused what I should call, as this will become part of the core library developers in the team. Here is the class member profile.

Info: Extension utility for String types. Overloads of this method can execute the same characters except a space [with what is set in the argument]
Purpose: Trim all intermediate or intermediate spaces to one place.
Example:

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 am reading] the guidelines of the Framework Framework, but this seems to bother me.

Some options that I think.

TrimIntermediate();  
TrimInbetween();

Here is the code in the request:

It is 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;
    }
}
+3
9

CollapseSpaces?

+14

CollapseSpaces , CollapseDelimiters CollapseWhitespace, .

+7

, ...

, . ( , , , , , , .)

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);
    }
}
+6
+2

NormalizeWhitespace? , , . , "Collapse" , .

+2
source

Try this, it works for me and seems to be much 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 such:

Console.WriteLine(input.NormalizeWhitespace(' '));
+1
source

CollapseExtraWhitespace

0
source

PaulaIsBrilliant, of course!

0
source

How makeCompact?

0
source

Source: https://habr.com/ru/post/1721290/


All Articles