想要从string中移除空格部分,有多少种方法,下面介绍jdk原生自带的方法,不包含第三方工具类库中的类似方法
trim() : 删除字符串开头和结尾的空格。
strip() : 删除字符串开头和结尾的空格。
stripleading() : 只删除字符串开头的空格
striptrailing() : 只删除字符串的结尾的空格
replace() : 用新字符替换所有目标字符
replaceall() : 将所有匹配的字符替换为新字符。此方法将正则表达式作为输入,以标识需要替换的目标子字符串
replacefirst() : 仅将目标子字符串的第一次出现的字符替换为新的字符串
需要注意的最重要的一点是,在java中string对象是不可变的,这意味着我们不能修改字符串,因此以上所有的方法我们得到的都是一个新的字符串。
trim()trim()是java开发人员最常用的删除字符串开头和结尾的空格方法
public class stringtest { public static void main(string[] args) { string stringwithspace = " hello word java "; stringtest.trimtest(stringwithspace); } private static void trimtest(string stringwithspace){ system.out.println("before trim : \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.trim(); system.out.println("after trim : \'" + stringaftertrim + "\'"); } }
输出结果
before trim : ' hello word java '
after trim : 'hello word java'
通过使用trim方法,原字符串的开头和结尾部分的空格已经被删除。其实,trim移除的空白字符指的是指ascii值小于或等于32的任何字符(’ u+0020 '):
strip()jdk 11的发行版中,添加了新的strip()方法来删除字符串中的前导和末尾空格。
trim方法只能针对ascii值小于等于32的字符进行移除,但是根据unicode标准,除了ascii中的字符以外,还是有很多其他的空白字符的。
而且为了识别这些空格字符,从java 1.5开始,还在character类中添加了新的iswhitespace(int)方法。该方法使用unicode来标识空格字符。
而在java 11中新增的这个strip方法就是使用这个character.iswhitespace(int)方法来判断是否为空白字符并删除它们的:
strip示例
public class stringtest { public static void main(string args[]) { string stringwithspace ='\u2001' + " hello word java " + '\u2001'; system.out.println("'" + '\u2001' + "' is space : " + character.iswhitespace('\u2001')); stringtest.striptest(stringwithspace); } private static void striptest(string stringwithspace){ system.out.println("before strip : \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.strip(); system.out.println("after strip : \'" + stringaftertrim + "\'"); } }
结果
' ' is space : true
before strip : ' hello word java '
after strip : 'hello word java'
java 11 中的 strip() 方法要比trim()方法更加强大,他可以移除很多不在ascii中的空白字符,判断方式就是通过character.iswhitespace()方法。
trim() 和 strip() 方法的区别
trimstrip
java 1 引入 java 11 引入
使用ascii 使用unicode值
删除开头和结尾的空白字符 删除开头和结尾的空白字符
删除ascii值小于或等于’u+0020’或’32’的字符 根据unicode删除所有空白字符
stripleading() 和 striptrailing()stripleading()和striptrailing()方法也都是在java 11中添加的。作用分别是删除字符串的开头的空格以及删除字符串的末尾的空格。
stripleading、striptrailing也使用character.iswhitespace(int)来标识空白字符。用法也和strip类似:
public class stringtest { public static void main(string args[]) { string stringwithspace ='\u2001' + " hello word java " + '\u2001'; system.out.println("'" + '\u2001' + "' is space : " + character.iswhitespace('\u2001')); stringtest.stripleadingtest(stringwithspace); stringtest.striptrailingtest(stringwithspace); } private static void stripleadingtest(string stringwithspace){ system.out.println("删除开头的空白字符"); system.out.println("before stripleading : \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.stripleading(); system.out.println("after stripleading : \'" + stringaftertrim + "\'"); } private static void striptrailingtest(string stringwithspace){ system.out.println("删除结尾的空白字符"); system.out.println("before striptrailing : \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.striptrailing(); system.out.println("after striptrailing : \'" + stringaftertrim + "\'"); } }
输出结果:
' ' is space : true
删除开头的空白字符
before stripleading : ' hello word java '
after stripleading : 'hello word java '
删除结尾的空白字符
before striptrailing : ' hello word java '
after striptrailing : ' hello word java'
replacereplace是从java 1.5中添加的,可以用指定的字符串替换每个目标子字符串。
此方法替换所有匹配的目标元素
public class stringtest { public static void main(string args[]) { string stringwithspace =" hello word java "; stringtest.replacetest(stringwithspace); } private static void replacetest(string stringwithspace){ system.out.println("before replace : \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.replace(" ", ""); system.out.println("after replace : \'" + stringaftertrim + "\'"); } }
结果:
before replace : ' hello word java '
after replace : 'hellowordjava'
使用replace方法可以替换掉字符串中的所有空白字符。需要特别注意的是,和trim方法一样,replace方法只能替换ascii中的空白字符。
replaceallreplaceall是jdk 1.4中添加的最强大的字符串操作方法之一。我们可以将这种方法用于许多目的。
使用replaceall()方法,我们可以使用正则表达式来用来识别需要被替换的目标字符内容。使用正则表达式,就可以实现很多功能,如删除所有空格,删除开头空格,删除结尾空格等等。
\s+ 所有的空白字符
^\s+ 字符串开头的所有空白字符
\s+$ 字符串结尾的所有空白字符
在java中要添加\我们必须使用转义字符,所以对于\s+ 我们必须使用 \\s+
replaceall(regex, “”); // 将正则表达式匹配到的内容,替换为""
public class stringtest { public static void main(string args[]) { string stringwithspace =" hello word java "; stringtest.replacealltest(stringwithspace," "); stringtest.replacealltest(stringwithspace,"\\s+"); stringtest.replacealltest(stringwithspace,"^\\s+"); stringtest.replacealltest(stringwithspace,"\\s+$"); } private static void replacealltest(string stringwithspace,string regex){ system.out.println("before replaceall with '"+ regex +"': \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.replaceall(regex, ""); system.out.println("after replaceall with '"+ regex +"': \'" + stringaftertrim + "\'"); } }
before replaceall with ' ': ' hello word java '
after replaceall with ' ': 'hellowordjava'
before replaceall with '\s+': ' hello word java '
after replaceall with '\s+': 'hellowordjava'
before replaceall with '^\s+': ' hello word java '
after replaceall with '^\s+': 'hello word java '
before replaceall with '\s+$': ' hello word java '
after replaceall with '\s+$': ' hello word java'
replacefirstreplacefirst方法也是在jdk1.4中添加的,它只将给定正则表达式的第一个匹配项替换为替换字符串。
public class stringtest { public static void main(string args[]) { string stringwithspace =" hello word java "; stringtest.replacefirsttest(stringwithspace," "); stringtest.replacefirsttest(stringwithspace,"\\s+"); stringtest.replacefirsttest(stringwithspace,"^\\s+"); stringtest.replacefirsttest(stringwithspace,"\\s+$"); } private static void replacefirsttest(string stringwithspace,string regex){ system.out.println("before replacefirst with '"+ regex +"': \'" + stringwithspace + "\'"); string stringaftertrim = stringwithspace.replacefirst(regex, ""); system.out.println("after replacefirst with '"+ regex +"': \'" + stringaftertrim + "\'"); } }
结果:
before replacefirst with ' ': ' hello word java '
after replacefirst with ' ': ' hello word java '
before replacefirst with '\s+': ' hello word java '
after replacefirst with '\s+': 'hello word java '
before replacefirst with '^\s+': ' hello word java '
after replacefirst with '^\s+': 'hello word java '
before replacefirst with '\s+$': ' hello word java '
after replacefirst with '\s+$': ' hello word java'
以上就是java中string字符串删除空格的方式有哪些的详细内容。