Given the following strings as example I need to find all combinations in a string with spaces removed.
Example string "INV/001 /01/ 2021 /S" contains 3 spaces, so combinations are:
INV/001 /01/ 2021 /S
INV/001 /01/ 2021/S
INV/001 /01/2021 /S
INV/001 /01/2021/S
INV/001/01/ 2021 /S
INV/001/01/ 2021/S
INV/001/01/2021 /S
INV/001/01/2021/S
Example string "INV/002/01/ 2021 /S" contains 2 spaces, so combinations are:
INV/001 /01/2021 /S
INV/001 /01/2021/S
INV/001/01/2021 /S
INV/001/01/2021/S
the catch is that i don't know how many space will be in string.
At this point i can find only 3 results and its very static and remove only one space. Probably i could use string.Replace to remove all space to get additional result.
string text = "INV/001 /01/ 2021 /S";
int i = 0;
List<int> indexOfText = new List<int>();
while ((i = text.IndexOf(" ", i)) != -1)
{
indexOfText.Add(i);
i += " ".Length;
}
for (int j = 0; j < indexOfText.Count; j++)
{
string newText = text.ReplaceAt(indexOfText[j], 1, "");
Console.WriteLine(newText);
}
Console.ReadKey();
Result:
INV/001/01/ 2021 /S
INV/001 /01/2021 /S
INV/001 /01/ 2021/S
public static class StringExtension
{
//// str - the source string
//// index- the start location to replace at (0-based)
//// length - the number of characters to be removed before inserting
//// replace - the string that is replacing characters
public static string ReplaceAt(this string str, int index, int length, string replace)
{
return str.Remove(index, Math.Min(length, str.Length - index))
.Insert(index, replace);
}
}
I would like to have some more generic way to remove spaces.