2011-05-31 129 views
5

我想創建一個字符串來代替*的所有空格,但我無法弄清楚如何做到這一點。誰能幫忙?字符串中的替換字符

String phrase = new String ("This is a String test."); 
+2

你使用哪種語言?在C#中它會是'var result = phrase.Replace('','*');' – 2011-05-31 01:07:15

回答

4

Mystring = Mystring.Replace('','*');

+2

你怎麼知道它是C#?沒有這樣的String構造函數需要另一個字符串。 – 2011-05-31 01:11:36

+0

謝謝!它效果很好! – Chickadee 2011-05-31 01:32:18

+0

@Bala R 2如此接近誰在乎?更換或更換。大不了。 :) – 2011-05-31 15:56:31

10

假設它是Java中,你可以使用String.replace方法:

phrase = phrase.replace(' ', '*'); 
1
String phrase = new String ("This is a String test."); 

/*Replace the Spaces with the *, */ 

String finalString = phrase.Replace(' ', '*');  

System.out.println(finalString); 
+0

我相信「替換」並不是以大寫字母開頭,至少,Eclipse對我說:P。除此之外,它就像一個魅力!謝謝! – ivanleoncz 2016-10-16 22:56:42

1

不要運營商創建的字符串。在Java中,String是一個特殊的類。所以

String phrase = "This is a String test."; 

就夠了。創建字符串運算符將創建兩次字符串。