1

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.

Cowa
  • 113
  • 1
  • 7
  • 2
    It sounds like this is a homework task, is that right? – Ackdari Feb 01 '21 at 14:26
  • no its not, why would you ask that? I just couldn't do it myself, couldn't find any answers – Cowa Feb 01 '21 at 14:31
  • Shouldn't the 3rd result for the first one be `INV/001 /01/2021/S` instead of `INV/001/ 01/2021/S`? – juharr Feb 01 '21 at 14:35
  • 1
    Why do you need to do it? Are you trying to compare the string with something else? Because there are probably better solutions if the response is yes – xanatos Feb 01 '21 at 15:26
  • Yes, its string comparation, but bank statement sometimes add random additional spaces in section where documents number is. Document number section can have spaces, but I cannot detect with one was there.To find correct document number I need to check every combination. – Cowa Feb 01 '21 at 15:31
  • 2
    You can "cheat" with regexes perhaps /* this one can be cached in static */ var spaceReplacer = new Regex(" +"); var pattern = spaceReplacer.Replace(Regex.Escape("INV/001 /01/ 2021 /S"), " *"); var rx = new Regex(pattern); var ismatch = rx.IsMatch("INV/001/01/2021 /S"); – xanatos Feb 01 '21 at 15:38
  • Great alternative! I checked it and this approach is suitable for me. I wouldn't think you could use RegEx like this. Wish I could mark your post as solution. – Cowa Feb 01 '21 at 15:59
  • You better go check this question https://stackoverflow.com/questions/4718965/c-sharp-string-comparison-ignoring-spaces-carriage-return-or-line-breaks if you also care about performance, regex is elegant but no so performant. – Alexey Rumyantsev Feb 01 '21 at 16:40
  • 2
    @Cowa You asked for something. You received the answer for that thing. But what you asked wasn't what you needed. It was a classical [XY](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem. The response you received is perfect for what you asked. – xanatos Feb 01 '21 at 17:08

1 Answers1

2

If you want to make universal solution, you have to go recursive.

My solution results in 8 combinations in first case and 4 in second.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        string text = "INV/001 /01/ 2021 /S";
        int i = 0;
        List<string> combinations = new List<string>();
        ComputeCombinations(combinations, text, 0, "");
        
        foreach(string str in combinations)
            Console.WriteLine(str);
        Console.ReadKey();
    }

    static void ComputeCombinations(
        List<string> result, string source, int pos, string prefix)
    {
        for (int i = pos; i < source.Length; i++)
        {
            if(source[i] != ' ')
                continue;
            ComputeCombinations(result, source, i + 1, 
                prefix + source.Substring(pos, i - pos + 1));
            ComputeCombinations(result, source, i + 1, 
                prefix + source.Substring(pos, i - pos));
            return;
        }

        if (pos < source.Length)
            result.Add(prefix + source.Substring(pos));
    }
}