问题
I have a problem with a String. The string is like this:
string something = " whatever and something else ";
Now I'm trying to get rid of the spaces at the beginning and at the end like this:
something = something.Trim();
But that didn't work, I also tried this:
something = something.TrimStart();
something = something.TrimEnd();
And this:
something = something.TrimStart(' ');
something = something.TrimEnd(' ');
And this:
int lineLength = line.Length;
string LastCharacter = line.Remove(lineLength - 1);
while (LastCharacter == " ")
{
line = line.Remove(lineLength - 1);
lineLength = line.Length;
LastCharacter = line.Remove(lineLength - 1);
}
The String is Out of a RichTextBox.
Now I think it could be a problem with the Text formatting or something (I'm in Germany).
Thank you in advance, tietze111
回答1:
here's something that will rip out all white space:
string something = " whatever ";
List<char> result = something.ToList();
result.RemoveAll(c => c == ' ');
something = new string(result.ToArray());
ok, try this for beginning and end only trims:
static string TrimWhitespace(string theString)
{
theString = " some kind of string example ";
theString = theString.TrimEnd();
theString = theString.TrimStart();
// MessageBox.Show(theString, "");
return theString;
}
来源:https://stackoverflow.com/questions/11778309/string-trim-spaces-doesnt-work