[英]Android. Replace * character from String
我有一個String變量,其中包含'*'。 但在使用之前我必須更換所有這個角色。
我嘗試過replaceAll函數,但沒有成功:
text = text.replaceAll("*","");
text = text.replaceAll("*",null);
有人能幫助我嗎? 謝謝!
為什么不使用String#replace()
方法,不使用regex
作為參數: -
text = text.replace("*","");
相反, String#replaceAll()
將正則表達式作為第一個參數,因為*
是正則表達式中的元字符 ,所以您需要將其轉義,或者在字符類中使用它。 所以,你這樣做的方式是: -
text = text.replaceAll("[*]",""); // OR
text = text.replaceAll("\\*","");
但是,你真的可以在這里使用簡單的替換 。
你可以簡單地使用String#replace()
text = text.replace("*","");
String.replaceAll(regex,str)將正則表達式作為第一個參數,因為*
是一個元變量,你應該用反斜杠轉義它以將其視為普通的字符。
text.replaceAll("\\*", "")
試試這個。
您需要使用轉義正則表達式的*
。
text = text.replaceAll("\\*","");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.