删除多个空格java代码_Java String.trim()会删除多less个空格?

所有这些 。

返回 :删除了前导空白和尾随空白的string的副本,如果没有前导或尾随空白,则为该string。

〜从Java 1.5.0文档引用

(但你为什么不试试看,自己看?)

从源代码(反编译):

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); }

你可以看到的两个字符意味着unicode低于空格字符的所有字符,在开始和结束时被删除。

如有疑问,请写一个unit testing:

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

NB :当然,testing(对于JUnit + Hamcrest)不会失败

有一点要指出的是,String.trim有一个特殊的“空白”定义。 它不会删除Unicode空格,而且还会删除您不认为空白的ASCII控制字符。

这个方法可以用来修剪string的开始和结尾的空白。 实际上,它也修剪所有的ASCII控制字符。

如果可能的话,你可能需要使用Commons Lang的StringUtils.strip(),它也处理Unicode空白(也是无效的)。

请参阅String类的API :

返回string的副本,省略前导和尾随空白。

两边的空格被删除:

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

String original = " content "; String withoutWhitespace = original.trim(); // original still refers to " content " // and withoutWhitespace refers to "content"

基于这里的Java文档, .trim()取代了通常被称为空白的“\ u0020”。

但是请注意,“\ u00A0”( Unicode NO-BREAK SPACE   )也被看作是一个空格,而.trim()不会删除它。 这在HTML中特别常见。

要删除它,我使用:

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

这里讨论了这个问题的一个例子。

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.

从Java文档(string类源),

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

* If this String object represents an empty character * sequence, or the first and last characters of character sequence * represented by this String object both have codes * greater than '\u0020' (the space character), then a * reference to this String object is returned. *

* Otherwise, if there is no character with a code greater than * '\u0020' in the string, then a new * String object representing an empty string is created * and returned. *

* Otherwise, let k be the index of the first character in the * string whose code is greater than '\u0020', and let * m be the index of the last character in the string whose code * is greater than '\u0020'. A new String * object is created, representing the substring of this string that * begins with the character at index k and ends with the * character at index m-that is, the result of * this.substring(km+1). *

* 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; }

请注意,获取开始和长度后,它会调用String类的子string方法。

trim()将删除所有前导和尾随空白。 但要注意:你的string没有改变。 trim()将会返回一个新的string实例。

如果您的stringinput是:

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

是的,输出将是“abc”; 但是,如果您的stringinput是:

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

输出将是This is a test所以修剪只会删除第一个字符之前和string中的最后一个字符后的空格,并忽略内部空间。 这是我的代码片略微优化内置的String修剪方法删除内部空间,并删除string中的第一个和最后一个字符之前和之后的空格。 希望能帮助到你。

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

它将删除双方的所有空间。

一个非常重要的事情是一个完全由“空格”组成的string将返回一个空string。

如果一个string sSomething = "xxxxx" ,其中x表示空白, sSomething.trim()将返回一个空string。

如果一个string sSomething = "xxAxx" ,其中x表示空白, sSomething.trim()将返回A

如果sSomething ="xxSomethingxxxxAndSomethingxElsexxx" , sSomething.trim()将返回SomethingxxxxAndSomethingxElse ,请注意单词之间的x数不会改变。

如果你想要一个整洁的string结合trim()正则expression式在这篇文章中显示: 如何删除string中使用Java重复的空格? 。

顺序对结果没有意义,但trim()首先会更有效率。 希望能帮助到你。

要只保留一个string的实例,可以使用下面的代码。

str = " Hello ";

要么

str = str.trim();

那么str的值就是str = "Hello"

修剪()适用于双方。

Javadoc的string有所有的细节。 从两端删除空格(空格,制表符等)并返回一个新的string。

如果你想检查一下什么方法,你可以使用BeanShell 。 这是一种脚本语言,旨在尽可能接近Java。 一般来说,它是一些放松的Java解释。 Groovy语言是这种types的另一种select。 这两种脚本语言都提供了从解释语言中获得的方便的Read-Eval-Print循环。 所以你可以运行控制台,只需键入:

" content ".trim();

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值