0

看下面的代码:

String comment = "1)FCR pick up in Hong Kong2)Local charges will be paiy in Hong Kong & in HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-23021977Fax:00852-2730217Transaction865320submittedVirginiaWong(T1281954U005) and Status is INCMP  on 10-JUN-11 11.28.45.764386 PM -05:00";
        //comment = comment.replaceAll("\\)", "\\\\)");
        //comment = comment.replaceAll("\\(", "\\\\(");
          if(comment == null || comment.length() < 100)
          {
            System.out.println();  
          }
         String[] strArray =    comment.split(" ");
         for (int i = 0; i < strArray.length; i++) 
           { 
              if(strArray[i].length() > 100)
               {
                 int iter = strArray[i].length() / 100 ;
                 int count = 100 ;
                 int initCount = 0 ;
                 String strReplace = null;

                    for(int j =0 ; j< iter ; j++)
                    {
                      strReplace = strArray[i].substring(initCount ,count); 

                      String strToReplace =  strReplace + "\n" ;
                      comment = comment.replaceAll(strReplace,strToReplace);
                      //comment = comment.replaceAll("\\)", "\\\\)");
                      //comment = comment.replaceAll("\\(", "\\\\(");
                      //comment = comment.replaceAll("\\\\", "");
                      System.out.println(comment);
                      System.out.println(comment.contains("\n"));   
                      initCount = count; //+1 ; 
                      count = count +100 ;
                    } 

                }   

            }
    }


当我运行时,出现以下异常:

线程“main”中的异常 java.util.regex.PatternSyntaxException: Unmatched close ')'
near index 4 HK$.3)Booking:virginiawong@fahkco.com.hk4)FCR&DOC:emilywu@fahkco.com.hkTel:00852-
23021977Fax :00852-2

据我了解,我必须逃避括号'(',')',我试图这样做(查看代码中的注释部分)没有任何异常,但我附加到字符串的换行符似乎没有出现。

4

1 回答 1

3

String.replaceAll对第一个参数使用正则表达式,当解释为正则表达式时,诸如此类的字符具有特殊)含义。

试试String.replace吧。(它仍然替换所有出现的给定子字符串。)

于 2012-07-30T05:46:00.003 回答