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!
CodePudding user response:
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);
}
...
CodePudding user response:
The program doesn't "delete space" physically. This line:
someString = string.Concat(before, after);
Does two things:
- Take the before and after substrings and concatenate them, obtaining a new value of type string (the value returned from the Concat function).
- Make an assignment of that new value to the existing string variable someString. Hence, the previous value stored in someString (which contains spaces) is discarded, and replaced by this new value.
CodePudding user response:
The before
string is the string before the whitespace character and the after
string is the string after the whitespace character, and both of them exclude the whitespace character, so the new string is the same as the previous with the whitespace character cut out. That is why the end result doesnt have any whitespace characters.