简体   繁体   English

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

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

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()删除这些边上的所有空格还是每边一个空格?

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?) (但你为什么不亲自尝试一下呢?)

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 while that you can see mean all the characters whose unicode is below the space character's, at beginning and end, are removed.您可以看到的两个while表示在开头和结尾处 unicode 低于空格字符的所有字符都被删除。

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)不会失败

One thing to point out, though, is that String.trim has a peculiar definition of "whitespace".不过,需要指出的一件事是 String.trim 对“空白”有一个特殊的定义。 It does not remove Unicode whitespace, but also removes ASCII control characters that you may not consider whitespace.它不会删除 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 空格(并且也是空安全的)。

See API for 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"

Based on the Java docs here , the .trim() replaces '\ ' which is commonly known as whitespace.根据此处的 Java 文档, .trim()替换了通常称为空格的 '\ '。

But take note, the '\ ' ( Unicode NO-BREAK SPACE &nbsp; ) is also seen as a whitespace, and .trim() will NOT remove this.但请注意, '\ ' ( Unicode NO-BREAK SPACE &nbsp; )也被视为空格,并且.trim()不会删除它。 This is especially common in HTML.这在 HTML 中尤为常见。

To remove it, I use :要删除它,我使用:

tmpTrimStr = tmpTrimStr.replaceAll("\\u00A0", "");

An example of this problem was discussed here . 此处讨论了此问题的一个示例。

Example of Java trim() removing spaces: Java trim()删除空格的示例:

public class Test
{
    public static void main(String[] args)
    {
        String str = "\n\t This is be trimmed.\n\n";

        String newStr = str.trim();     //removes newlines, tabs and spaces.

        System.out.println("old = " + str);
        System.out.println("new = " + newStr);
    }
}

OUTPUT输出

old = 
 This is a String.


new = This is a String.

From java docs(String class source),来自 java docs(String class source),

/**
 * Returns a copy of the string, with leading and trailing whitespace
 * omitted.
 * <p>
 * If this <code>String</code> object represents an empty character
 * sequence, or the first and last characters of character sequence
 * represented by this <code>String</code> object both have codes
 * greater than <code>'&#92;u0020'</code> (the space character), then a
 * reference to this <code>String</code> object is returned.
 * <p>
 * Otherwise, if there is no character with a code greater than
 * <code>'&#92;u0020'</code> in the string, then a new
 * <code>String</code> object representing an empty string is created
 * and returned.
 * <p>
 * Otherwise, let <i>k</i> be the index of the first character in the
 * string whose code is greater than <code>'&#92;u0020'</code>, and let
 * <i>m</i> be the index of the last character in the string whose code
 * is greater than <code>'&#92;u0020'</code>. A new <code>String</code>
 * object is created, representing the substring of this string that
 * begins with the character at index <i>k</i> and ends with the
 * character at index <i>m</i>-that is, the result of
 * <code>this.substring(<i>k</i>,&nbsp;<i>m</i>+1)</code>.
 * <p>
 * This method may be used to trim whitespace (as defined above) from
 * the beginning and end of a string.
 *
 * @return  A copy of this string with leading and trailing white
 *          space removed, or this string if it has no leading or
 *          trailing white space.
 */
public String trim() {
int len = count;
int st = 0;
int off = offset;      /* avoid getfield opcode */
char[] val = value;    /* avoid getfield opcode */

while ((st < len) && (val[off + st] <= ' ')) {
    st++;
}
while ((st < len) && (val[off + len - 1] <= ' ')) {
    len--;
}
return ((st > 0) || (len < count)) ? substring(st, len) : this;
}

Note that after getting start and length it calls the substring method of String class.请注意,在获取 start 和 length 后,它会调用 String 类的 substring 方法。

trim() will remove all leading and trailing blanks. trim()将删除所有前导和尾随空格。 But be aware: Your string isn't changed.但请注意:您的字符串没有改变。 trim() will return a new string instance instead. trim()将返回一个新的字符串实例。

If your String input is:如果您的字符串输入是:

String a = "   abc   ";
System.out.println(a);

Yes, output will be, "abc";是的,输出将是,“abc”; But if your String input is:但是如果你的字符串输入是:

String b = "    This  is  a  test  "
System.out.println(b);

Output will be This is a test So trim only removes spaces before your first character and after your last character in the string and ignores the inner spaces.输出将是This is a test因此,trim 仅删除字符串中第一个字符之前和最后一个字符之后的空格,并忽略内部空格。 This is a piece of my code that slightly optimizes the built in String trim method removing the inner spaces and removes spaces before and after your first and last character in the string.这是我的一段代码,它稍微优化了内置的String trim 方法,删除内部空格并删除字符串中第一个和最后一个字符前后的空格。 Hope it helps.希望能帮助到你。

public static String trim(char [] input){
    char [] output = new char [input.length];
    int j=0;
    int jj=0;
    if(input[0] == ' ' )    {
        while(input[jj] == ' ') 
            jj++;       
    }
    for(int i=jj; i<input.length; i++){
      if(input[i] !=' ' || ( i==(input.length-1) && input[input.length-1] == ' ')){
        output[j]=input[i];
        j++;
      }
      else if (input[i+1]!=' '){
        output[j]=' ';
        j++;
      }      
    }
    char [] m = new char [j];
    int a=0;
    for(int i=0; i<m.length; i++){
      m[i]=output[a];
      a++;
    }
    return new String (m);
  }

To keep only one instance for the String, you could use the following.要仅保留 String 的一个实例,您可以使用以下内容。

str = "  Hello   ";

or或者

str = str.trim();

Then the value of the str String, will be str = "Hello"那么str字符串的值,将是str = "Hello"

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

One very important thing is that a string made entirely of "white spaces" will return a empty string.一件非常重要的事情是,完全由“空格”组成的字符串将返回一个空字符串。

if a string sSomething = "xxxxx" , where x stand for white spaces, sSomething.trim() will return an empty string.如果string sSomething = "xxxxx" ,其中x代表空格, sSomething.trim()将返回一个空字符串。

if a string sSomething = "xxAxx" , where x stand for white spaces, sSomething.trim() will return A .如果string sSomething = "xxAxx" ,其中x代表空格, sSomething.trim()将返回A

if sSomething ="xxSomethingxxxxAndSomethingxElsexxx" , sSomething.trim() will return SomethingxxxxAndSomethingxElse , notice that the number of x between words is not altered.如果sSomething ="xxSomethingxxxxAndSomethingxElsexxx"sSomething.trim()将返回SomethingxxxxAndSomethingxElse ,注意单词之间的x数量没有改变。

If you want a neat packeted string combine trim() with regex as shown in this post: How to remove duplicate white spaces in string using Java?如果您想要一个整洁的打包字符串,请将trim()与正则表达式结合起来,如这篇文章所示: 如何使用Java 删除字符串中的重复空格? . .

Order is meaningless for the result but trim() first would be more efficient.顺序对结果毫无意义,但首先使用trim()会更有效。 Hope it helps.希望能帮助到你。

String formattedStr=unformattedStr;
formattedStr=formattedStr.trim().replaceAll("\\s+", " ");

Trim() 对双方都有效。

Javadoc for String has all the details. String 的Javadoc包含所有详细信息。 Removes white space (space, tabs, etc ) from both end and returns a new string.从两端删除空格(空格、制表符等)并返回一个新字符串。

If you want to check what will do some method, you can use BeanShell .如果你想检查什么会做一些方法,你可以使用BeanShell It is a scripting language designed to be as close to Java as possible.它是一种旨在尽可能接近 Java 的脚本语言。 Generally speaking it is interpreted Java with some relaxations.一般来说,它被解释为 Java 有一些放松。 Another option of this kind is Groovy language.这种类型的另一种选择是Groovy语言。 Both these scripting languages provide convenient Read-Eval-Print loop know from interpreted languages.这两种脚本语言都提供了从解释语言知道的方便的 Read-Eval-Print 循环。 So you can run console and just type:所以你可以运行控制台并输入:

"     content     ".trim();

You'll see "content" as a result after pressing Enter (or Ctrl+R in Groovy console).Enter (或 Groovy 控制台中的Ctrl+R )后,您将看到"content"作为结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM