Reputation: 36773
"\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"
Upvotes: 2
Views: 126
Reputation: 700840
Replace all white space blocks with a single space:
myString = Regex.Replace(myString, @"\s+", " ").Trim();
Upvotes: 10
Reputation: 8232
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.
Upvotes: 0
Reputation: 36624
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.
Upvotes: 0
Reputation: 1756
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);
Upvotes: 7