2012-03-16 2 views
0
final String message = 
         "Please enter the following code in the password reset screen:\n" + 
           "\n"+ 
           "{CODE} (((((" + codeStr + ")))))\n" + 
           "If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it." 
           .replace("{CODE}", codeStr); 

이 암호 재설정 화면에서 다음 코드를 입력하세요아주 간단한 문자열이 실패

결과 : {CODE} (((((5RE2GT4FWH))))) 당신은하지 않았다 경우 이 코드를 요청하거나이 전자 메일이 무엇인지 모르는 경우에는 무시해도됩니다.

어떻게 간단하게 대체 할 수 있습니까? 나는 어쩌면 문제가 {CODE}의 두 인스턴스가 비슷하게 보였지만 실제로 다른 문자로 구성되어 있었지만 다른 하나 위에 복사 한 다음 수정하지 않았다고 생각했습니다.

답변

2

당신은 마지막 String 개체의 replace()를 호출된다 : ["If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it."].

임시 변수에 [String, replace()] 앞에 저장하고 다시 시도하거나 괄호를 사용하십시오.

6

요청은 알고있다 : 때문에 괄호 부족

final String message = 
        (
          "Please enter the following code in the password reset screen:\n" + 
          "\n"+ 
          "{CODE}" + codeStr + "\n" + 
          "If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it." 
        ) 
        .replace("{CODE}", codeStr); 

가 먼저 이루어진 문자열의 마지막 부분에서 수행 한 후에야 함께 연결된 부분 교체했다 하였다.

+1

당신이 당신의 자신의 질문에 대답하기 위해 지금은 모든 사람들이 당신에게 무료 upvotes을주고 관리 방법과 같은 I) 내가 t를 호출하고있어 – jzworkman

2

당신은 전체 문자열에 대체 호출 할 필요가 :

final String message = 
     ("Please enter the following code in the password reset screen:\n" + 
     "\n"+ "{CODE} (((((" + codeStr + ")))))\n" + 
     "If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it." 
     ).replace("{CODE}", codeStr); 
0

의 마지막 문자는 replace()이며 문자열 리터럴로만 부릅니다. 이 같은 전체 메시지의 주위에 괄호가 필요합니다

final String message = 
    ("Please enter the following code in the password reset screen:\n" + 
    "\n"+ 
    "{CODE} (((((" + codeStr + ")))))\n" + 
    "If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it." 
    ) 
    .replace("{CODE}", codeStr); 
+1

그의 무승부와 모두를위한 upvote 축제에 갔다. – Bohemian

0

이것은 사용하는 일이 오래된 자바 코드 스타일을 업그레이드 혜택을 누릴 것입니다 솔루션의 종류 (오래된,하지만이보다 최신) java.util.Formatter

final String message = 
         "Please enter the following code in the password reset screen:\n" + 
           "\n"+ 
           "{CODE} (((((" + codeStr + ")))))\n" + 
           "If you didn't ask for this code, or don't know what this e-mail is about, you can safely ignore it." 
           .replace("{CODE}", codeStr); 

는 될 것

Formatter formatter = new Formatter(new StringBuilder, Locale.US); 
final String message = formatter.format("Please enter the following code in the password reset screen:\n\n %s (((((%s)))))\n", codeStr); 
관련 문제