C# 删除变量中的空格

发布于 2024-09-02 05:15:57 字数 209 浏览 9 评论 0原文

我的变量看起来像:

name = "Lola                                 "; // notice the whitespace

如何删除末尾的空格,只留下“Lola”?


谢谢大家,但是 .Trim() 对我不起作用。

我从文件中读取文本,如果有帮助的话。

My variable looks like:

name = "Lola                                 "; // notice the whitespace

How can I delete the whitespace at the end to leave me with just "Lola"?


Thank you all, but .Trim() don't work to me.

I read the text from a file, if that is any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

‘画卷フ 2024-09-09 05:15:57

使用 Trim()

 string name = "Lola       "
 Console.WriteLine(name.Trim()); // puts out 'Lola'

use Trim().

 string name = "Lola       "
 Console.WriteLine(name.Trim()); // puts out 'Lola'
荆棘i 2024-09-09 05:15:57

如果空格始终位于字符串的末尾,请使用:

name = name.TrimEnd();

如果空格也可能位于字符串的开头,则使用:

name = name.Trim();

If the space will always be at the end of the string, use:

name = name.TrimEnd();

If the space may also be at the beginning, then use:

name = name.Trim();
演多会厌 2024-09-09 05:15:57

Trim 不会更改字符串,它会创建一个新的修剪副本。这就是为什么看起来 name.Trim(); 没有做任何事情——你正在丢弃结果。

相反,使用 name = name.Trim(); 作为 ICR 建议

Trim doesn't change the string, it creates a new trimmed copy. That is why it seems like name.Trim(); isn't doing anything -- you are throwing away the results.

Instead, use name = name.Trim(); as ICR suggests.

小瓶盖 2024-09-09 05:15:57

答案是.Trim()。请注意,由于字符串是不可变的,因此您必须分配操作的结果:

name = "Lola                                 ";
name.Trim();  // Wrong: name will be unchanged
name = name.Trim();  // Correct: name will be trimmed

The answer is .Trim(). Note that since strings are immutable, you have to assign the result of the operation:

name = "Lola                                 ";
name.Trim();  // Wrong: name will be unchanged
name = name.Trim();  // Correct: name will be trimmed
从﹋此江山别 2024-09-09 05:15:57

使用 Trim() 方法。

Use the Trim() method.

如梦亦如幻 2024-09-09 05:15:57

只需用于

string.Trim()

删除字符串中的所有空格即可;

string.TrimEnd()

从字符串末尾删除空格

string.TrimStart()

从字符串开头删除空格

Just use

string.Trim()

to remove all spaces from your string;

string.TrimEnd()

to remove spaces from the end of your string

string.TrimStart()

to remove spaces from the start of your string

ˇ宁静的妩媚 2024-09-09 05:15:57

使用修剪()。
所以你只能得到“Lola”

use Trim().
So u can get only "Lola"

薄情伤 2024-09-09 05:15:57

或者这就是你想要做的?

string name = "Lola ...long space...";
Console.WriteLine(name.Substring(0, name.IndexOf(" ") - 1)); // puts out 'Lola'

Or is this what you meant to do?

string name = "Lola ...long space...";
Console.WriteLine(name.Substring(0, name.IndexOf(" ") - 1)); // puts out 'Lola'
污味仙女 2024-09-09 05:15:57

如果您想删除字符串中任何位置的所有空格,可以使用 Replace()

string name = "   Lola Marceli    ";
Console.WriteLine(name.Replace(" ", "")); // puts out 'LolaMarceli'

If you wanted to remove all spaces anywhere in the string, you could use Replace()

string name = "   Lola Marceli    ";
Console.WriteLine(name.Replace(" ", "")); // puts out 'LolaMarceli'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文