Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串修剪空格不';行不通_C#_String_Space_Trim_Spaces - Fatal编程技术网

C# 字符串修剪空格不';行不通

C# 字符串修剪空格不';行不通,c#,string,space,trim,spaces,C#,String,Space,Trim,Spaces,我对字符串有问题。 字符串如下所示: string something = " whatever and something else "; something = something.Trim(); 现在,我试图在开始和结束时消除空格,如下所示: string something = " whatever and something else "; something = something.Trim(); 但那不起作用,我也试过: something = something.

我对字符串有问题。 字符串如下所示:

string something = "  whatever and something else  ";
something = something.Trim();
现在,我试图在开始和结束时消除空格,如下所示:

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);
}
这是:

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

这里有一些东西将删除所有空白:

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

这里有一些东西将删除所有空白:

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

你用什么语言<代码>字符串如下:…你说的“like”是什么意思?这就是你的绳子吗?如果不是,那么字符串从何处获取,以及转换为int?时每个字符的值是多少。Trim()应该按预期工作,您使用的是正确的。问题出现在字符串中的文本中。感谢您的回答,文本来自richTextBox,很抱歉我忘记了语言,是C#您使用的是什么语言<代码>字符串如下:…你说的“like”是什么意思?这就是你的绳子吗?如果不是,那么字符串从何处获取,以及转换为int?时每个字符的值是多少。Trim()应该按预期工作,您使用的是正确的。问题在于字符串中包含的文本。感谢您的回答,文本来自richTextBox,很抱歉我忘记了语言,很抱歉,我只想删除开头和结尾的空格。我发现您的代码使用的是相同的TrimEnd和TrimStart函数。我刚刚测试了我发布的内容,它确实有效。如果没有,请告诉我(团队)。谢谢你的回答,但我认为这不会奏效,因为我已经尝试了几乎相同的方法。对不起,这是我的错误,修剪()后添加了一个空格,因此无法正常工作,谢谢你的回答!很抱歉,我只想删除开头和结尾的空格。我发现您的代码使用的是相同的TrimEnd和TrimStart函数。我刚刚测试了我发布的内容,它确实有效。如果没有,请告诉我(团队)。谢谢你的回答,但我认为这不会奏效,因为我已经尝试了几乎相同的方法。对不起,这是我的错误,修剪()后添加了一个空格,因此无法正常工作,谢谢你的回答!