日期:2014-05-20 浏览次数:20702 次
String str = "I am a Chinese people." ; String newStr=""; String strArr[] = str.spit(" "); for(int i=0;i<strArr.length;i++){ if(str.equals("Chinese")){ strArr[i]="Beijing"; } } for(int i=0;i<strArr.length;i++){ newStr+=strArr[i]+" "; }
------解决方案--------------------
return "I am a Chinese people.".replace("Chinese", "Beijing");
------解决方案--------------------
public class Test { public static void main(String args[]) { String s = "I am a Chinese people"; s = s.replaceAll("Chinese","Beijing"); System.out.println(s); } }
------解决方案--------------------
String str="I am a Chinese people." ;
String newStr=str.replaceAll("Chinese", "Beijing")
------解决方案--------------------
String str = "I am a Chinese people.";
String replace = str.replaceAll("Chinese", "Beijing");
System.out.print(replace);
------解决方案--------------------
public class ChangeString { /** * @param args */ public static void main(String[] args) { System.out.println(ChangeString.method1()); } public static String method1(){ return "I am a Chinese people".replace("Chinese", "Beijing"); } }
------解决方案--------------------
楼上说的方法都行。
要注意的是String类里的replaceAll方法参数的字符串用的是正则表达式,不是普通字符串。只不过“Chinese”的正则表达式就是“Chinese”。