Search code examples
c#stringsubstringindexof

Deleting Spaces In Complicated Strings


Just another silly novice question (like Confucius said it is better to ask then live in ignorance) regards to deletion of spaces in long and complicated strings. First of all I must say that I am trying to do exercises from the book: C# 7.0: All-In-One-For-dummies and the following example I took from this book. Here is the code:

internal class Program
    {
        static void Main(string[] args)
        {
            string someString = " this is a\nstring. ";
            char[] whiteSpace = {' ', '\n', '\t' };
            Console.WriteLine("Here is how the string looks before:" + someString);
            Console.WriteLine("And here is how it looks after: ");
            for( ; ; )
            {
                int offset = someString.IndexOfAny(whiteSpace);
                if (offset == -1)
                {
                    break;
                }
                string before = someString.Substring(0, offset);
                string after = someString.Substring(offset + 1);
                someString = string.Concat(before, after);
            }
            Console.WriteLine(someString);

            Console.WriteLine("\nPress any key to terminate the program. ");
            Console.ReadLine();
        }
    } 

Output is like this: "thisisastring" But why? Can anybode explain it? Everything looks quite understandable for me until the line with two substrings ("before" and "after") being concatenated after. Why infinite loop deletes all spaces? Substring() method deletes nothing as far as I know and Concat() too. Variable "before" is the just the part before the space and variable "after" is the part after the space. In which moment program deletes space?

Thanks in advance for any suggestions!


Solution

  • Let me try to explain:

    ...
    // Infinite loop; common alternative form is "while (true) {...}" 
    for( ; ; )
    {
        // looking for the leftmost index of any char from whiteSpace array
        int offset = someString.IndexOfAny(whiteSpace);
        
        // if no whitespace found ...
        if (offset == -1)
        {
            // ... stop processing the string
            break;
        }
        
        // from now on offset is the index of the leftmost whitespace character 
    
        // we remove leftmost whitespace character: someString = before + after
        // note, that before is a substring before whitespace; 
        // after is a substring after whitespace
    
        // before - we take all characters in [0..offset) range; 
        // note, that char at offset index - i.e. whitespace character - is excluded
        string before = someString.Substring(0, offset);
    
        // after - we take all characters starting from offset + 1 -
        // again we don't take whitespace character  which is at offset index 
        string after = someString.Substring(offset + 1);
    
        // finally, we combine all characters except the leftmost whitespace,
        // i.e. character which is on offset index
        someString = string.Concat(before, after);
    }
    ...