我有一个涉及替换方法的问题。我在这里看到了类似这样的问题,但我尝试做替换第一,但它对我不起作用。有没有,我可以使用replace方法来更改导致的字符串:Helle,Werld;使用替换方法将其生成为“Hello,World”。有没有办法使用replaceFirst方法来搜索“le”的序列并将其替换为“lo”并将“We”更改为“Wo”?请参阅下面的代码:
public class Printer
{
/**Description: Replacement class
*
*
*/
public static void main(String[] args)
{
String test1Expected = "Hello, World!";
String newString1;
String test1 = "Holle, Werld!";
newString1 = test1.replace('o', 'e');
//Could I do: newString1.replaceFirst("le","lo);
System.out.println("newString1 = " + newString1);
//Output comes out to "Helle, Werld!"
}
}
答案 0 :(得分:0)
你可以一个接一个地分开两个正则表达式。请尝试以下
newString1 = newString1.replaceAll("le", "lo").replaceAll("We", "Wo");