Java String.trim() 将删除多少个空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2198875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:38:58  来源:igfitidea点击:

How many spaces will Java String.trim() remove?

javastringtrim

提问by oneat

In Java, I have a String like this:

在 Java 中,我有一个这样的字符串:

"     content     ".

Will String.trim()remove all spaces on these sides or just one space on each?

String.trim()删除这些边上的所有空格还是每边一个空格?

采纳答案by LukeH

All of them.

所有这些

Returns: A copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

~ Quoted from Java 1.5.0 docs

返回: 此字符串的副本,其中删除了前导和尾随空格,或者此字符串(如果它没有前导或尾随空格)。

~ 引自 Java 1.5.0 文档

(But why didn't you just try it and see for yourself?)

(但你为什么不亲自尝试一下呢?)

回答by Ravia

It will remove all spaces on both the sides.

它将删除两侧的所有空格。

回答by tangens

trim()will remove all leading and trailing blanks. But be aware: Your string isn't changed. trim()will return a new string instance instead.

trim()将删除所有前导和尾随空格。但请注意:您的字符串没有改变。trim()将返回一个新的字符串实例。

回答by ZeissS

Trim() works for both sides.

Trim() 对双方都有效。

回答by glmxndr

From the source code (decompiled) :

从源代码(反编译):

  public String trim()
  {
    int i = this.count;
    int j = 0;
    int k = this.offset;
    char[] arrayOfChar = this.value;
    while ((j < i) && (arrayOfChar[(k + j)] <= ' '))
      ++j;
    while ((j < i) && (arrayOfChar[(k + i - 1)] <= ' '))
      --i;
    return (((j > 0) || (i < this.count)) ? substring(j, i) : this);
  }

The two whilethat you can see mean all the characters whose unicode is below the space character's, at beginning and end, are removed.

while您可以看到的两个表示在开头和结尾处 unicode 低于空格字符的所有字符都被删除。

回答by dfa

When in doubt, write a unit test:

如有疑问,请编写单元测试:

@Test
public void trimRemoveAllBlanks(){
    assertThat("    content   ".trim(), is("content"));
}

NB: of course the test (for JUnit + Hamcrest) doesn't fail

注意:当然测试(对于 JUnit + Hamcrest)不会失败

回答by Juha Syrj?l?

See APIfor String class:

请参阅String 类的API

Returns a copy of the string, with leading and trailing whitespace omitted.

返回字符串的副本,省略前导和尾随空格。

Whitespace on both sides is removed:

两边的空白被删除:

Note that trim()does not change the String instance, it will return a new object:

请注意,trim()不会更改 String 实例,它将返回一个新对象:

 String original = "  content  ";
 String withoutWhitespace = original.trim();

 // original still refers to "  content  "
 // and withoutWhitespace refers to "content"

回答by fastcodejava

Javadocfor String has all the details. Removes white space (space, tabs, etc ) from both end and returns a new string.

String 的Javadoc包含所有详细信息。从两端删除空格(空格、制表符等)并返回一个新字符串。

回答by Rorick

If you want to check what will do some method, you can use BeanShell. It is a scripting language designed to be as close to Java as possible. Generally speaking it is interpreted Java with some relaxations. Another option of this kind is Groovylanguage. Both these scripting languages provide convenient Read-Eval-Print loop know from interpreted languages. So you can run console and just type:

如果你想检查什么会做一些方法,你可以使用BeanShell。它是一种旨在尽可能接近 Java 的脚本语言。一般来说,它被解释为 Java 有一些放松。这种类型的另一种选择是Groovy语言。这两种脚本语言都提供了从解释语言知道的方便的 Read-Eval-Print 循环。所以你可以运行控制台并输入:

"     content     ".trim();

You'll see "content"as a result after pressing Enter(or Ctrl+Rin Groovy console).

"content"Enter(或Ctrl+R在 Groovy 控制台中)后,您将看到结果。

回答by Thilo

One thing to point out, though, is that String.trim has a peculiar definition of "whitespace". It does not remove Unicode whitespace, but also removes ASCII control characters that you may not consider whitespace.

不过,需要指出的一件事是 String.trim 对“空白”有一个特殊的定义。它不会删除 Unicode 空格,但也会删除您可能不考虑空格的 ASCII 控制字符。

This method may be used to trim whitespace from the beginning and end of a string; in fact, it trims all ASCII control characters as well.

此方法可用于从字符串的开头和结尾修剪空格;事实上,它还修剪了所有 ASCII 控制字符。

If possible, you may want to use Commons Lang's StringUtils.strip(), which also handles Unicode whitespace (and is null-safe, too).

如果可能,您可能希望使用 Commons Lang 的 StringUtils.strip(),它也处理 Unicode 空白(并且也是空安全的)。