String Trim Spaces doesn't work

淺唱寂寞╮ 提交于 2020-01-17 01:56:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!