How would you approach this simple string parsing problem?
"\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"
Replace all white space blocks with a single space:
myString = Regex.Replace(myString, @"\s+", " ").Trim();
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);
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.
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.
精彩评论