在字符串中替换字符
我正在尝试创建一个字符串,将所有空格替换为*但我无法确切地知道如何做到这一点。 有人可以帮忙吗?
String phrase = new String ("This is a String test.");
假设它是Java,您可以使用String.replace方法:
phrase = phrase.replace(' ', '*');
Mystring = Mystring.Replace(”,’*’);
String phrase = new String ("This is a String test."); /*Replace the Spaces with the *, */ String finalString = phrase.Replace(' ', '*'); System.out.println(finalString);
不要使用new运算符创建String。 在Java中,String是一个特殊的类。 所以
String phrase = "This is a String test.";
足够。 使用new运算符创建String将创建两次字符串。