作者:博士吃货 | 来源:互联网 | 2023-09-23 15:42
篇首语:本文由编程笔记#小编为大家整理,主要介绍了java中string.trim()函数的作用相关的知识,希望对你有一定的参考价值。
trim /[tr?m] / 英文意思:整理,修理,修剪,整齐的
trim()的作用:去掉字符串首尾的空格。
public static void main(String arg[]){
String a=" hello world ";
String b="hello world";
System.out.println(b.equals(a));
a=a.trim();//去掉字符串首尾的空格
System.out.println(a.equals(b));
}
执行结果:
a: hello world ,false
a:hello world,true
trim()的源代码:
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
while ((st )) {
st++;
}
while ((st )) {
len--;
}
return ((st > 0) || (len this;
}