它工作正常,所有形式。
public static void main(String[] args) {
test(4, "FOUR", "");
test(4, "FOUR", "Hello");
test(4, "FOUR", "4");
test(4, "FOUR", "1 2 3 4 5 6");
test(4, "FOUR", "123456");
test(4, "FOUR", "Test43");
}
private static void test(int num, String numText, String string) {
System.out.println("\"" + string + "\" -> \"" + string.replace(num + "", numText) + "\"" +
" = \"" + string.replace("" + num, numText) + "\"" +
" = \"" + string.replace(String.valueOf(num), numText) + "\"");
}
輸出是:
"" -> "" = "" = ""
"Hello" -> "Hello" = "Hello" = "Hello"
"4" -> "FOUR" = "FOUR" = "FOUR"
"1 2 3 4 5 6" -> "1 2 3 FOUR 5 6" = "1 2 3 FOUR 5 6" = "1 2 3 FOUR 5 6"
"123456" -> "123FOUR56" = "123FOUR56" = "123FOUR56"
"Test43" -> "TestFOUR3" = "TestFOUR3" = "TestFOUR3"
你是什麼意思由它不工作?你能給一個樣本輸入和你得到的輸出嗎? – Codebender
您需要將其替換爲字符。如果你不想明確地轉換它,'String.replace(「」+ 4,「FOUR」)應該可以工作。 –
@AaronD,和他已經有什麼不同?我認爲改變這裏的秩序不應該有所作爲。 – Codebender