java中trim_JAVA中trim()函数使用方法
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中間
String?str?=?"?hell?o?";
String?str2?=?str.replaceAll("?",?"");
System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
4.str = .replaceAll("\\s*", "");
可以替換大部分空白字符, 不限于空格
\s 可以匹配空格、制表符、換頁符等空白字符的其中任意一個
5.或者下面的代碼也可以去掉所有空格,包括首尾、中間
public?String?remove(String?resource,char?ch)
{
StringBuffer?buffer=new?StringBuffer();
int?position=0;
char?currentChar;
while(position
{
currentChar=resource.charAt(position++);
if(currentChar!=ch)?buffer.append(currentChar);?}?return?buffer.toString();
}
測試的全部代碼如下:
public?class?test1?{
public?static?void?main(String[]?args)?{
String?QJstr?=?"?hello?wch?";
String?QJstr1?=?remove(QJstr,'?');
System.out.println(QJstr?+?"\n"?+?QJstr1);
}
public?static?String?remove(String?resource,char?ch)
{
StringBuffer?buffer=new?StringBuffer();
int?position=0;
char?currentChar;
while(position
{
currentChar=resource.charAt(position++);
if(currentChar!=ch)?buffer.append(currentChar);
}
return?buffer.toString();
}
}
標簽:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,尊重他人勞動成果
文章轉(zhuǎn)載自:網(wǎng)絡轉(zhuǎn)載
2
好文不易,鼓勵一下吧!
總結(jié)
以上是生活随笔為你收集整理的java中trim_JAVA中trim()函数使用方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LabVIEW如何打开Acrobat P
- 下一篇: 民办大学计算机专业教师,考生被民办大学录