2017-10-19 20 views
1

을 대체하지만, 나는이 모든 Replace symbol "%" with word "Percent" 아무것도 나를 위해 일한 노력했다찾아 자바 1.8.0 내가 %를 대체하려고에서 %

String str = "%28Sample text%29"; 

    str.replaceAll("%29", "\\)"); 

    str.replaceAll("%28", "\\("); 
    System.out.println("Replaced string is " + str); 

일치하지 않는 자바. 미리 감사드립니다.

+0

사용'replace'합니다. –

+1

'replaceAll'은 원래'String' 객체를 변경하지 않습니다. 대신 결과는 새로운'String' 객체로 반환됩니다. – Alex

답변

4

효과가 있습니다. 당신은 str

str = str.replaceAll("%29", "\\)"); 

    str = str.replaceAll("%28", "\\("); 
0

Jerry06's answer에 할당 할 다시해야 할 올바른 것입니다.

그러나 간단히 URLDecoder을 사용하여 유니 코드 값을 디코딩하면됩니다.

String s = "%28Hello World!%29"; 
s = URLDecoder.decode(s, "UTF-8"); 
System.out.println(s); 

윌 출력 : (! 안녕하세요)

0

문제는 당신이 replaceall의 사용을 오해이다. regex 기반 대체품입니다. 당신이 사용해야하는 것은 정상적인 replace 방법이 같다 : 당신이 실제로 정규식과 일치 할 필요는 없습니다

String str = "%28Sample text%29"; 

str=str.replace("%29", "\\)"). replace("%28", "\\("); 
System.out.println("Replaced string is " + str);