删除复杂字符串中的空格
这只是另一个愚蠢的新手问题(就像孔子所说的,宁问不如无知),关于删除长而复杂的字符串中的空格。首先,我必须说,我正在尝试从《C# 7.0:All-In-One-For-dummies》这本书中进行练习,以及我从这本书中获取的以下示例。代码如下:
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();
}
}
输出如下:“thisisastring” 但为什么呢?有谁能解释一下吗? 一切对我来说看起来都很容易理解,直到带有两个子字符串(“之前”和“之后”)的行被连接起来。为什么无限循环会删除所有空格?据我所知,Substring() 方法不会删除任何内容,Concat() 也不会删除任何内容。变量“before”是空格之前的部分,变量“after”是空格之后的部分。程序在哪一刻删除了空格?
预先感谢您的任何建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我尝试解释:
Let me try to explain:
该程序不会在物理上“删除空间”。这一行:
做两件事:
The program doesn't "delete space" physically. This line:
Does two things:
字符串之前的
是whitespace字符之前的字符串,
之后的是whitespace字符之后的字符串,并且两个都排除了whitespace字符,因此新字符串为与以前的白空字符相同。这就是为什么最终结果没有任何空格字符的原因。
The
before
string is the string before the whitespace character and theafter
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.