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("\\]", "");
完成!
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("\\]", "");
完成!
留言
張貼留言