String Class의 replaceAll 메서드를 사용할 경우 '$' 로 인해 java.lang.IllegalArgumentException 가 발생할 수 있다. replaceAll 내부에서 matcher 를 사용하기 때문인 듯 하며 해결 방안은
replaceAll을 통해 원하는 메세지로 변경하기 전에 먼저 아래 굵은 글씨 부분을 처리한다.
public class StringTest3 {
public StringTest3() {}
public void replaceAll2(String str) {
try {
String message = "My title is This, 'This is test for String.replaceAll()'.";
String result = message.replaceAll("title", str);
System.out.println(result);
} catch( Exception e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StringTest3 st = new StringTest3();
String text = "USD mark$";
String convertedText = text.replaceAll("[\\$]", "\\\\\\$");
st.replaceAll2(convertedText);
}
}
replaceAll을 통해 원하는 메세지로 변경하기 전에 먼저 아래 굵은 글씨 부분을 처리한다.
public class StringTest3 {
public StringTest3() {}
public void replaceAll2(String str) {
try {
String message = "My title is This, 'This is test for String.replaceAll()'.";
String result = message.replaceAll("title", str);
System.out.println(result);
} catch( Exception e ) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StringTest3 st = new StringTest3();
String text = "USD mark$";
String convertedText = text.replaceAll("[\\$]", "\\\\\\$");
st.replaceAll2(convertedText);
}
}
댓글을 달아 주세요