String Trim Spaces不起作用

时间:2012-08-02 13:20:56

标签: c# string space trim spaces

我遇到String问题。 字符串是这样的:

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

String不在RichTextBox中。

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

提前谢谢你, tietze111

1 个答案:

答案 0 :(得分: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;
    }