2013년 2월 17일 일요일

java replace 실행시 주의사항들


펌글: http://mean79.tistory.com/60

1. []으로 싸주면 문자자체로 인식하는 것들.

*  ⇒ [*]
+  ⇒ [+]
$  ⇒ [$]
|  ⇒ [|]              "너 때문에 집에를 못갔다 ㅡㅜ "


2. \\를 붙여줘야 하는 것들.

( ⇒ \\(
) ⇒ \\)
{ ⇒ \\{
} ⇒ \\}
^ ⇒ \\^
[ ⇒ \\[
] ⇒ \\]


3. 자바의 특수문자는 \을 쓴다.

 " ⇒ \"


4. 나머지 부호들은 괜찮은 듯 하다.

확인된 것.

! # % & @ ` : ; - . < > , ~ '



ex ) 위에 놈들 다 지워 보자.

    String c = "!\"#$%&(){}@`*:+;-.<>,^~|'[]";
    c = c.replaceAll("!\"#[$]%&\\(\\)\\{\\}@`[*]:[+];-.<>,\\^~|'\\[\\]", "");




 public static String getSTRFilter(String str){
  int str_length = str.length();
  String strlistchar   = "";
  String str_imsi   = "";
  String []filter_word = {"","\\.","\\?","\\/">\\~","\\!","\\@","\\#","\\$","\\%","\\^","\\&","\\*","\\(","\\)","\\_","\\+","\\=","\\|","\\\\","\\}","\\]","\\{","\\[","\\\"","\\'","\\:","\\;","\\<","\\,","\\>","\\.","\\?","\\/"};

  for(int i=0;i<filter_word.length;i++){
   //while(str.indexOf(filter_word[i]) >= 0){
      str_imsi = str.replaceAll(filter_word[i],"");
      str = str_imsi;
   //}
  }

  return str;

 }


아님 replace가 안 먹을때는 이런방법도..^^
import java.util.StringTokenizer;

public class WebUtil
{
           // 문자열 변환  String a= "abc" => replace(a, "c") => a : ab
           public String strReplace(String s1, String s2){
                      String res = "";
                      StringTokenizer str = new StringTokenizer(s1, s2);


                       while(str.hasMoreTokens()){
                                     res += str.nextToken();  
                               System.out.println(res);
                       }
               return res;
           }
}

- naver

댓글 없음:

댓글 쓰기