代碼如下:
public class TempTest { public static void main(String[] args) { //string去除空格 String str=" hello world "; System.out.println(str); String str1=str.trim();//去除首尾空格 System.out.println(str1); String str2=str.replace(" ","");//去掉所有空格,包括首尾,中間 System.out.println(str2); String str3=str.replaceAll(" +","");//去掉所有空格,包括首尾,中間 System.out.println(str3); String str4=str.replaceAll("\\s*",""); //可以替換大部分空白字符, 不限于空格 . 說明:\s 可以匹配空格、制表符、換頁符等空白字符的其中任意一個(gè) System.out.println(str4); //string去除標(biāo)點(diǎn)符號(hào) //正則表達(dá)式去除標(biāo)點(diǎn) String stri="ss&*(,.~1如果@&(^-自己!!知道`什`么#是$苦%……Z,&那*()么一-=定——+告訴::;\"'/?.,><[]{}\\||別人什么是甜。"; System.out.println(stri); String stri1=stri.replaceAll("\\p{Punct}","");//不能完全清除標(biāo)點(diǎn) System.out.println(stri1); String stri2=stri.replaceAll("\\pP","");//完全清除標(biāo)點(diǎn) System.out.println(stri2); String stri3=stri.replaceAll("\\p{P}","");//同上,一樣的功能 System.out.println(stri3); String stri4=stri.replaceAll("[\\pP\\p{Punct}]","");//清除所有符號(hào),只留下字母 數(shù)字 漢字 共3類. System.out.println(stri4); } }