2012-08-28 2 views
0

가능한 중복 :
Converting Symbols, Accent Letters to English Alphabet자바 - 교체 여러 characteres

나는 자바 프로그래머가 아니에요 내가 아닌 특별한 사람과 특별한 characteres을 대신 꿔. 내가하고있는 일은 myString.toLowerCase().replace('ã','a').replace('é','a').replace('é','e')...이지만이 작업을 수행하는 더 간단한 방법이 될 것이라고 확신합니다.

나는 PHP와 함께 작동하도록 사용하고 있는가 자바에서 그런 일을이 str_replace 기능

// Provides: You should eat pizza, beer, and ice cream every day 
$phrase = "You should eat fruits, vegetables, and fiber every day."; 
$healthy = array("fruits", "vegetables", "fiber"); 
$yummy = array("pizza", "beer", "ice cream"); 

$newphrase = str_replace($healthy, $yummy, $phrase); 

이있다? 또는 적어도 내가 바꾸고 싶은 모든 문자에 replace을 사용하는 것보다 쉬운 것.

+0

당신은이에 대해 정규 표현식을 사용할 수 있습니다 http://stackoverflow.com/questions/2608205/replace -special-characters-in-java – Jay

답변

4

를 사용할 수 있습니다 의 JDK와 함께 제공됩니다,하지만 당신은 쉽게 당신이 더 편리 발견하면 스스로를 만들 수 있습니다 여기에 답변을 참조하십시오 :

public static String strReplace(String[] from, String[] to, String s){ 
    for(int i=0; i<from.length; i++){ 
    s = s.replaceAll(from[i], to[i]); 
    } 
    return s; 
} 
+0

질문에 대한 나의 이해에서 OP가 찾고있다. 어떤 문자가 아닌 * 특별한 * 문자를 대체하는 방법. – Jay

+0

그는 str_replace PHP 함수에 상응하는 것에 관심이있을 것이라고 말했기 때문에 쉽게 생성하는 방법을 보여주었습니다. – kgautron

+0

그 정도면 충분합니다. 공유해 주셔서 감사합니다. – Jay

1

나는 오랫동안 자바를 사용하지 않은,하지만 당신은 항상 내가 배열에없는 str_replace 동등한 생각하지 않습니다 (모든 언어) 교체의 배열 ...

char[] specials = "ãéé".toCharArray(); 
char[] replacements = "aee"; 
for (int i = 0; i < specials.length; i++) { 
    myString.replaceAll(specials[i], replacements[i]); 
}