如何删除字符串开头或结尾的所有空格?
如何删除字符串开头和结尾的所有空格?
就像这样:
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
String.Trim()
返回一个字符串等于输入字符串,其中包含所有 空格< /a> 从开头和修剪:String.TrimStart()
返回一个从开头修剪空格的字符串:String.TrimEnd()
code> 返回一个从末尾删除空格的字符串:这些方法都不会修改原始字符串对象。
(至少在某些实现中,如果没有要修剪的空格,您将返回与开始时相同的字符串对象:
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:String.TrimStart()
returns a string with white-spaces trimmed from the start:String.TrimEnd()
returns a string with white-spaces trimmed from the end: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.)
看看
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.修剪
现在是“Hello”
trimmed
is now"Hello"
使用
String.Trim()
函数。use the
String.Trim()
function.使用 String.Trim 方法。
Use
String.Trim
method.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.
修剪()
从当前字符串中删除所有前导和尾随空白字符。
修剪(字符)
从当前字符串中删除字符的所有前导和尾随实例。
Trim(Char[])
从当前字符串中删除数组中指定的一组字符的所有前导和尾随匹配项。请看我从 Microsoft 文档页面引用的以下示例。
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.