2012-03-16 10 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つのインスタンスが同じように見えたが、実際は異なる文字で構成されていたということだと思っていましたが、もう一方をコピーして貼り付けました。

答えて

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."]。

一時変数に[Stringreplace()の前に]を格納して再試行するか、かっこを使用してください。

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を与えているかのようになります彼のドローと誰のためにupvoteの祭りに行った – 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

;

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); 
Bohemian

0

これはjava.util.Formatter

(古いが、これより新しい)を使用して1に、この古いJavaコードのスタイルをアップグレードの恩恵を受けるソリューションの一種であります私はトンを呼んでいる)
関連する問題