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