java 去除空格 標點符號

2021-08-01 00:22:43 字數 1510 閱讀 3468

public class temptest \\||別人什麼是甜。";

system.out.println(stri);

string stri1=stri.replaceall("\\p","");//不能完全清除標點

system.out.println(stri1);

string stri2=stri.replaceall("\\pp","");//完全清除標點

system.out.println(stri2);

string stri3=stri.replaceall("\\p","");//同上,一樣的功能

system.out.println(stri3);

string stri4=stri.replaceall("[\\pp\\p]","");//清除所有符號,只留下字母 數字 漢字 共3類.

system.out.println(stri4);

}}

執行結果:

hello   world  

hello world

helloworld

helloworld

helloworld

ss&*(,.~1如果@&(^-自己!!知道`什`麼#是$苦%……z,&那*()麼一-=定——+告訴::;"'/?.,><{}\||別人什麼是甜。

ss1如果自己知道什麼是苦……z,那麼一定——告訴別人什麼是甜。

ss~1如果^自己知道`什`麼是$苦z那麼一=定+告訴》<||別人什麼是甜

ss~1如果^自己知道`什`麼是$苦z那麼一=定+告訴》<||別人什麼是甜

ss1如果自己知道什麼是苦z那麼一定告訴別人什麼是甜

關於replace 和replaceall:

replace(char oldchar,char newchar)

replace(charsequence  target,charsequence  replacement)

replaceall(string regex,string replacement)

1)replace的引數是char和charsequence,即可以支援字元的替換,也支援字串的替換(charsequence即字串序列的意思,說白了也就是字串);

2)replaceall的引數是regex,即基於規則表示式的替換,比如,可以通過replaceall("\\d", "*")把乙個字串所有的數字字元都換成星號;

相同點是都是全部替換,即把源字串中的某一字元或字串全部換成指定的字元或字串,如果只想替換第一次出現的,可以使用 replacefirst(),這個方法也是基於規則表示式的替換,但與replaceall()不同的是,只替換第一次出現的字串;

另外,如果replaceall()和replacefirst()所用的引數據不是基於規則表示式的,則與replace()替換字串的效果是一樣的,即這兩者也支援字串的操作;

還有一點注意:執行了替換操作後,源字串的內容是沒有發生改變的.

英語標點符號大全

close brace,close curly 右花括號 open parenthesis,open paren 左圓括號 close parenthesis,close paren 右圓括號 brakets parentheses 括號 open bracket 左方括號 close bracke...

標點符號英文寫法

period 句號 ma 逗號 colon 冒號 semicolon 分號 exclamation 驚嘆號 question mark 問號 hyphen 連字元 apostrophe 省略號 所有格符號 dash 破折號 single quotation marks 單引號 double quot...

標點符號相關詞彙

逗號 comma 句號 full stop 頓號 pause mark 冒號 colon 分號 semicolon 問號 question mark 省略號 suspension points 嘆號 exclamation mark 書名號 book title mark 引號 quote 前括號 ...