如何删除字符串开头或结尾的所有空格?

发布于 2024-09-12 17:03:21 字数 243 浏览 7 评论 0原文

如何删除字符串开头和结尾的所有空格?

就像这样:

"hello" 返回 "hello"
“hello” 返回 “hello”
“你好”返回“你好”
“hello world” 返回“hello world”

How can I remove all white space from the beginning and end of a string?

Like so:

"hello" returns "hello"
"hello " returns "hello"
" hello " returns "hello"
" hello world " returns "hello world"

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

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

发布评论

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

评论(7

温柔戏命师 2024-09-19 17:03:21

String.Trim() 返回一个字符串等于输入字符串,其中包含所有 空格< /a> 从开头修剪:

"   A String   ".Trim() -> "A String"

String.TrimStart() 返回一个从开头修剪空格的字符串:

"   A String   ".TrimStart() -> "A String   "

String.TrimEnd() code> 返回一个从末尾删除空格的字符串:

"   A String   ".TrimEnd() -> "   A String"

这些方法都不会修改原始字符串对象。

(至少在某些实现中,如果没有要修剪的空格,您将返回与开始时相同的字符串对象:

csharp> string a = "a";
csharp>字符串修剪 = a.Trim();
csharp> (对象)a == (对象)修剪;
返回 true

我不知道这是否是由语言保证的。)

String.Trim() returns a string which equals the input string with all white-spaces trimmed from start and end:

"   A String   ".Trim() -> "A String"

String.TrimStart() returns a string with white-spaces trimmed from the start:

"   A String   ".TrimStart() -> "A String   "

String.TrimEnd() returns a string with white-spaces trimmed from the end:

"   A String   ".TrimEnd() -> "   A String"

None of the methods modify the original string object.

(In some implementations at least, if there are no white-spaces to be trimmed, you get back the same string object you started with:

csharp> string a = "a";
csharp> string trimmed = a.Trim();
csharp> (object) a == (object) trimmed;
returns true

I don't know whether this is guaranteed by the language.)

梦中楼上月下 2024-09-19 17:03:21

看看 Trim() 其中返回一个新字符串,并从调用它的字符串的开头和结尾删除空格。

take a look at Trim() which returns a new string with whitespace removed from the beginning and end of the string it is called on.

蘑菇王子 2024-09-19 17:03:21
string a = "   Hello   ";
string trimmed = a.Trim();

修剪现在是“Hello”

string a = "   Hello   ";
string trimmed = a.Trim();

trimmed is now "Hello"

浅紫色的梦幻 2024-09-19 17:03:21

使用String.Trim()函数。

string foo = "   hello ";
string bar = foo.Trim();

Console.WriteLine(bar); // writes "hello"

use the String.Trim() function.

string foo = "   hello ";
string bar = foo.Trim();

Console.WriteLine(bar); // writes "hello"
那小子欠揍 2024-09-19 17:03:21

使用 String.Trim 方法。

Use String.Trim method.

农村范ル 2024-09-19 17:03:21

String.Trim() 删除字符串开头和结尾的所有空格。
要删除字符串内的空格或规范化空格,请使用正则表达式。

String.Trim() removes all whitespace from the beginning and end of a string.
To remove whitespace inside a string, or normalize whitespace, use a Regular Expression.

GRAY°灰色天空 2024-09-19 17:03:21

修剪()
从当前字符串中删除所有前导和尾随空白字符。
修剪(字符)
从当前字符串中删除字符的所有前导和尾随实例。
Trim(Char[]) 从当前字符串中删除数组中指定的一组字符的所有前导和尾随匹配项。

请看我从 Microsoft 文档页面引用的以下示例。

char[] charsToTrim = { '*', ' ', '\''};
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed\n   {0}\nto\n   '{1}'", banner, result);

// The example displays the following output:
//       Trimmmed
//          *** Much Ado About Nothing ***
//       to
//          'Much Ado About Nothing'

Trim()
Removes all leading and trailing white-space characters from the current string.
Trim(Char)
Removes all leading and trailing instances of a character from the current string.
Trim(Char[]) Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.

Look at the following example that I quoted from Microsoft's documentation page.

char[] charsToTrim = { '*', ' ', '\''};
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed\n   {0}\nto\n   '{1}'", banner, result);

// The example displays the following output:
//       Trimmmed
//          *** Much Ado About Nothing ***
//       to
//          'Much Ado About Nothing'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文