直接上问题
String s="你好_,今天是_年_月_日.";
String name="某人";
String year="2012";
String month="03";
String day="16";通过字符串替换
s.replace(oldChar, newChar)
我希望得到的结果是
你好某人,今天是2012年03月16日.
请问怎么做?    

解决方案 »

  1.   


    public static void main(String[] args) {
    String s = "你好_,今天是_年_月_日.";
    String name = "某人";
    String year = "2012";
    String month = "03";
    String day = "16";
    String s_temp = "";
    // 查询出"_"的所有位置
    int path_1 = s.indexOf("_");// 第一个位置
    int path_2 = path_1 + s.substring(path_1 + 1).indexOf("_") + 1;// 第二个位置
    int path_3 = path_2 + s.substring(path_2 + 1).indexOf("_") + 1;// 第三个位置
    int path_4 = path_3 + s.substring(path_3 + 1).indexOf("_") + 1;// 第四个位置
    s_temp = s.substring(0, path_1) + name
    + s.substring(path_1 + 1, path_2) + year
    + s.substring(path_2 + 1, path_3) + month
    + s.substring(path_3 + 1, path_4) + day;
    System.out.println(s_temp); }
      

  2.   


    public static void main(String[] args) {
    String s = "你好_,今天是_年_月_日.";
    String name = "某人";
    String year = "2012";
    String month = "03";
    String day = "16";
    String s_temp = "";
    // 查询出"_"的所有位置
    int path_1 = s.indexOf("_");// 第一个位置
    int path_2 = path_1 + s.substring(path_1 + 1).indexOf("_") + 1;// 第二个位置
    int path_3 = path_2 + s.substring(path_2 + 1).indexOf("_") + 1;// 第三个位置
    int path_4 = path_3 + s.substring(path_3 + 1).indexOf("_") + 1;// 第四个位置
    s_temp = s.substring(0, path_1) + name
    + s.substring(path_1 + 1, path_2) + year
    + s.substring(path_2 + 1, path_3) + month
    + s.substring(path_3 + 1, path_4) + day
    + s.substring(path_4 + 1);
    System.out.println(s_temp); }
      

  3.   

    public static void main(String[] args) {
    String s = "你好_,今天是_年_月_日.";
    String name = "某人";
    String year = "2012";
    String month = "03";
    String day = "16";
    String s_temp = "";
    // 查询出"_"的所有位置
    int path_1 = s.indexOf("_");// 第一个位置
    int path_2 = path_1 + s.substring(path_1 + 1).indexOf("_") + 1;// 第二个位置
    int path_3 = path_2 + s.substring(path_2 + 1).indexOf("_") + 1;// 第三个位置
    int path_4 = path_3 + s.substring(path_3 + 1).indexOf("_") + 1;// 第四个位置
    s_temp = s.substring(0, path_1) + name
    + s.substring(path_1 + 1, path_2) + year
    + s.substring(path_2 + 1, path_3) + month
    + s.substring(path_3 + 1, path_4) + day
    + s.substring(path_4 + 1);
    System.out.println(s_temp); }
      

  4.   


     String name="某人";
     String year="2012";
     String month="03";
     String day="16";
     System.out.println(s.replaceFirst("_", name).replaceFirst("_", year).replaceFirst("_", month).replaceFirst("_", day));
      

  5.   

    用commons-lang包的StringUtilsimport org.apache.commons.lang.StringUtils;public class Test { /**
     * @param args
     */
    public static void main(String[] args) {
    String s = "你好_,今天是_年_月_日.";
    String name = "某人";
    String year = "2012";
    String month = "03";
    String day = "16"; s = StringUtils.replaceOnce(s, "_", name);
    s = StringUtils.replaceOnce(s, "_", year);
    s = StringUtils.replaceOnce(s, "_", month);
    s = StringUtils.replaceOnce(s, "_", day); System.out.println(s);
    }
    }
      

  6.   


    这个好,第一次知道有replaceFirst方法,以前一直用StringUtils的。
      

  7.   

    String s="你好_,今天是_年_月_日.";
    String name="某人";
    String year="2012";
    String month="03";
    String day="16";
    String arrayStr[] = {name,year,month,day};
    StringBuilder sb  = new StringBuilder();
    int i=0;
    for(String temp :s.split("_")){
    sb.append(temp);
    if(i<arrayStr.length)
    sb.append(arrayStr[i]);
    i++;
    }

    System.out.println(sb.toString()); 
      

  8.   


    String s="你好_,今天是_年_月_日.";
            Matcher matcher = Pattern.compile("_").matcher(s);
            String name ="某人";
            String year = "2012";
            String month = "03";
            String day = "15";
            String[] strs = new String[]{name, year, month, day};
            
            StringBuffer sb = new StringBuffer();
            int position = 0;
            while (matcher.find()) {
    matcher.appendReplacement(sb, strs[position++]);
    }
            matcher.appendTail(sb);
            System.out.println(sb);
      

  9.   

    String name="某人";
         String year="2012";
         String month="03";
         String day="16";
         System.out.println(s.replaceFirst("_", name).replaceFirst("_", year).replaceFirst("_", month).replaceFirst("_", day));
      

  10.   


                    String s="你好_,今天是_年_月_日.";
    String name="某人";
    String year="2012";
    String month="03";
    String day="16";
    String arrayStr[] = {name,year,month,day};
    StringBuilder sb  = new StringBuilder();
    int i=0;
    for(String temp :s.split("_")){
    sb.append(temp);
    if(i<arrayStr.length)
    sb.append(arrayStr[i]);
    i++;
    }

    System.out.println(sb.toString());