0

我有一个字符串的问题。字符串是这样的:

string something = "  whatever and something else  ";

现在我试图摆脱开头和结尾的空格,如下所示:

something = something.Trim();

但这没有用,我也试过这个:

something = something.TrimStart();
something = something.TrimEnd();

还有这个:

something = something.TrimStart(' ');
something = something.TrimEnd(' ');

还有这个:

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);
}

字符串不在 RichTextBox 中。

现在我认为这可能是文本格式或其他问题(我在德国)。

提前谢谢你, tietze111

4

1 回答 1

4

这是会撕掉所有空白的东西:

 string something = " whatever    ";
 List<char> result = something.ToList();
 result.RemoveAll(c => c == ' ');
 something = new string(result.ToArray());

好的,试试这个开始和结束只修剪:

  static string TrimWhitespace(string theString)
    {
        theString = "  some kind of string example ";
        theString = theString.TrimEnd();
        theString = theString.TrimStart();
        // MessageBox.Show(theString, "");
        return theString;
    }
于 2012-08-02T13:31:08.730 回答