您将如何解决这个简单的字符串解析问题?
"\n \n Expected:\n \n \n Q4\n \n \n 2011\n \n "
从该字符串中,我需要得到以下内容:
"Expected Q4 2011"
我已经尝试了以下内容,但没有骰子:
myString.Trim().Replace("\n", "");
我得到以下内容(大量空白是故意的,而不是站点格式化程序问题。这实际上是返回的内容。)
"Expected: Q4 2011"
"\n \n Expected:\n \n \n Q4\n \n \n 2011\n \n "
From that string, I need to get the following:
"Expected Q4 2011"
I've tried the following and no dice:
myString.Trim().Replace("\n", "");
I get the following (the massive whitespace is intentional and not a site formatter issue. That is in fact what is returned.)
"Expected: Q4 2011"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将所有空白块替换为单个空格:
Replace all white space blocks with a single space:
有多种方法可以做到这一点,但这里有一个简短的方法:
There are several ways to do this, but here's a short way:
尝试
假设您确定空格确实是空格字符。
Try
Assuming you are sure the spaces are really space characters.
如果公平地说您希望删除所有非字母数字字符(空格、标点符号和符号字符),则可以使用以下正则表达式:
\W
是\ w
,匹配所有字母数字(0-9、az、AZ)和下划线。If it is fair to say that you want all non-alphanumeric characters removed (whitespace, punctuation & symbol characters), you can use the following regular expression:
\W
is the inverse of\w
, which matches all alphanumerics (0-9, a-z, A-Z) and the underscore.