字符串修剪空间不起作用 [英] String Trim Spaces doesn't work

查看:40
本文介绍了字符串修剪空间不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

还有这个:

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 中.

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).

先谢谢你,tietze111

Thank you in advance, tietze111

推荐答案

这里有一些可以撕掉所有空白的东西:

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

这篇关于字符串修剪空间不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆