您将如何解决这个简单的字符串解析问题?

发布于 2024-11-07 07:50:37 字数 907 浏览 0 评论 0原文

"\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅语花开 2024-11-14 07:50:37

将所有空白块替换为单个空格:

myString = Regex.Replace(myString, @"\s+", " ").Trim();

Replace all white space blocks with a single space:

myString = Regex.Replace(myString, @"\s+", " ").Trim();
梦初启 2024-11-14 07:50:37

有多种方法可以做到这一点,但这里有一个简短的方法:

string foo = "\n \n Expected:\n \n \n Q4\n \n \n 2011\n \n ";
string[] foos = foo.Split(new char[] { ' ', '\n' },
                          StringSplitOptions.RemoveEmptyEntries);
string bar = string.Join(" ", foos);

There are several ways to do this, but here's a short way:

string foo = "\n \n Expected:\n \n \n Q4\n \n \n 2011\n \n ";
string[] foos = foo.Split(new char[] { ' ', '\n' },
                          StringSplitOptions.RemoveEmptyEntries);
string bar = string.Join(" ", foos);
不…忘初心 2024-11-14 07:50:37

尝试

var partsSplitByWhitespace = myString.Split(new[] {' ', '\n'}, 
                                                StringSplitOptions.RemoveEmptyEntries);
var combinedStringAgain = string.Join(" ", partsSplitByWhitespace);
var result = combinedStringAgain.Trim();

假设您确定空格确实是空格字符。

Try

var partsSplitByWhitespace = myString.Split(new[] {' ', '\n'}, 
                                                StringSplitOptions.RemoveEmptyEntries);
var combinedStringAgain = string.Join(" ", partsSplitByWhitespace);
var result = combinedStringAgain.Trim();

Assuming you are sure the spaces are really space characters.

紫瑟鸿黎 2024-11-14 07:50:37

如果公平地说您希望删除所有非字母数字字符(空格、标点符号和符号字符),则可以使用以下正则表达式:

string output = Regex.Replace(@"[\W_]+", myString, " ");

\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:

string output = Regex.Replace(@"[\W_]+", myString, " ");

\W is the inverse of \w, which matches all alphanumerics (0-9, a-z, A-Z) and the underscore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文