String s = "a = ? and b = ? and c = ? and d = ?";
String[] args = {1,2,3,4};
我要做的操作就是把字符串中的对应位置的?用数组对应位置的值替换,即1替换第一个位置的问号,2替换第二个位置的问号,依次类推最后得到的结果是 s = "a = 1 and b = 2 and c = 3 and d = 4";应该不是很难

解决方案 »

  1.   

    public class Test2 { public static void main(String[] args) {

    String s = "a = ? and b = ? and c = ? and d = ?";
    String[] str = {"1","2","3","4"}; System.out.println(Test2.replace(s, str, "\\?"));
    }


    public static String replace(String s,String[] str,String spit){

    String[] temp = s.split(spit);
    StringBuffer  sBuffer = new StringBuffer();

    for(int i=0;i<temp.length;i++){

    sBuffer.append(temp[i]).append(str[i]);
    }

    return sBuffer.toString();
    }}
      

  2.   

    迭代循环替换就是了····public static String a(StringBuilder sb, String[] array, int i) {
    int row = sb.lastIndexOf("?");
    if (row > -1) {
    i--;
    sb.delete(row, row + 1);
    sb.insert(row, array[i]);
    a(sb, array, i);
    }
    return sb.toString();
    }
    public static void main(String[] args) {
    String[] argss = {"1","2","3","4"};
    System.out.println(a( new StringBuilder("a = ? and b = ? and c = ? and d = ?"), argss, argss.length));
    }具体自己再去修改一下
      

  3.   

    happysmhzp兄的方法比较有用,多谢了!
      

  4.   

    public class Realize{

    public static void main(String[] args){

    String s="a = ? and b = ? and c = ? and d = ?";
    String[] sArray={"1","2","3","4"};
    for(int i=0;i<4;i++){
    s=s.replaceFirst("[?]",sArray[i]);
    }

    System.out.println(s);
    }
    }