trim():去掉字符串首尾的空格。
执行结果:a: hello world ,false
a:hello world,true
str.replace(" ","");去掉所有空格,包括首尾、中间
String str=" hell o wo rld ";
String str2=str.replaceAll();
System.out.println(str2);
执行结果:
hello world
本文介绍Java中处理字符串的方法,重点讲解如何使用trim()方法去除字符串两端的空白字符,以及使用replace()和replaceAll()方法去除字符串中的所有空白字符。通过实例演示了这些方法的具体应用。
trim():去掉字符串首尾的空格。
执行结果:a:hello world,true
str.replace(" ","");去掉所有空格,包括首尾、中间
String str=" hell o wo rld ";
String str2=str.replaceAll();
System.out.println(str2);
执行结果:
hello world
654

被折叠的 条评论
为什么被折叠?