2013-05-28 29 views
-2

我怎麼能改造string在與string.replace的Java

st = "[123, 123, 134, 90]" 

s = "123 123 134 90" 
+0

爲什麼不直接用替換所有,[和] 「」 ......(注意,如果你使用正則表達式,'['是一個元字符,需要用\轉義 - - 但它也是一個Java字符串轉義,所以使用\\\) – Patashu

回答

1

替換 「」 與 「」

替換 「」 用 「」

將「[」替換爲「」

取代 「]」 與 「」

+0

yes [with「」但我想替換「,」和「 [「,」]「所以我用replaceAll(」,\\] \\ [「,」「),但它不起作用,所以你可以爲我寫正則表達式? – Shah

+1

@agile你應該這樣做'newString = replaceAll(「\\ [」,「」).replaceAll(「\\]」,「」).replaceAll(「,」,「」);' – Maroun

+0

@Maroun非常感謝。它的工作原理:) – Shah

-1
public class Regex { 
    public static void main(final String[] args) { 
     String st = "[123, 123, 134, 90]"; 

     //replace '[' with blank 
     String a = st.replace("[", ""); 
     //replace ',' with blank 
     String b = a.replace(",", ""); 
     //replace ']' with blank 
     String c = b.replace("]", ""); 

     System.out.println(c); 
    } 
} 
+1

你需要添加一些文字與代碼,只是代碼是如此粗魯。 – Siddharth

+0

@Siddharth好嗎? :) – RishikeshDhokare

0

與感謝Maroun Maroun

String st = "[123, 123, 134, 90]"

String s = st.replaceAll("\\["," ").replaceAll("\\]"," ").replaceAll(","," ");