java replaceAll [ ] character issue

ArrayList item,
do somethine.......

String ori_txt = item.toString().trim();

最後把 item.toString() 轉成字串後.
會多出數個字元: ex: [所在縣市,醫院名稱,編號,醫院評鑑結果,教學醫院評鑑結果,醫院評鑑合格效期,教學醫院合格效期,醫院電話,地址]


要把 [, ] 兩字元移除

ori_txt=ori_txt.replaceAll("\n", "");
ori_txt=ori_txt.replaceAll("[", "");
ori_txt=ori_txt.replaceAll("]", "");

但出現error:

Caused by: java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 1: [

原來是[字元要跳脫.. 因為這欄位本來是放RegExp pattern是以[ ]開頭與結尾.

要用\\[ 與\\] 才可.

改成:
String ori_txt = item.toString().trim();
ori_txt = ori_txt.replaceAll("\n", "");
ori_txt = ori_txt.replaceAll("\\[", "");
ori_txt = ori_txt.replaceAll("\\]", "");

完成!

留言

這個網誌中的熱門文章

最爛的銀行服務-玉山銀行

Mark App Design Apps - Terms and Privacy Policy (服務條款,隱私權政策)

SMR疊瓦式hdd致命缺陷被解決????!!!

ios app 上架時app icon要注意事項

更改google drive預設存放目錄位置Change Google Drive Default Folder Location in Windows

google play 正式發布前測試報告...非常好用.

舊有app在Android 12 閃退問題& app Splash screens

app bundle and bundletool. 關於aab安裝問題

關於google play console app應用程式簽署

Google Play badge徽章產生器