有两个字符串:
str1="#1.jpg#2.gif#3.jpg#4.jpg";
str2="#1.jpg#3.gif";
合并后的字符串为:1.jpg#2.gif#3.gif#4.jpg
有没有简单点的方法,我自己写了好几个循环

解决方案 »

  1.   

    楼上的是什么意思,你是说str1是怎么合并出来的吗
      

  2.   

    简单的方法就是按规则拆开,然后放到HASHTABLE里面,让重名的自动覆盖,然后再取出来,这个办法不必费脑筋考虑算法。
      

  3.   

    3.jpg 和 3.gif 只出一个?
      

  4.   

    String str1="#1.jpg#2.gif#3.jpg#4.jpg";
    String str2="#5.jpg#6.gif";

    String[] arr = str2.split("#");
    for(int i=0;i<arr.length;i++)
    if(str1.indexOf(arr[i]) == -1)str1 += "#"+arr[i];
    System.out.println(str1);
      

  5.   

    感谢 shan1119(大天使,卐~解!) 的帮忙,只是并不是我要的答案,不是简单把两个字符串合并,str2中有3.gif, str1中也有3.jpg 数字相同但不是同一文件,所以要替换掉str1中的3.jpg为3.gif。 bigc2001(大C) 说的HASHTABLE中可让重名的自动覆盖,能否详细一些,小弟多谢各位了
      

  6.   

    str1和str2里面的数字是顺序的还是无序的? 
    如果str1="#1.jpg#2.gif#3.jpg#4.jpg";
    str2="#3.jpg#1.gif";
    应该合并出什么来? 还有str2会不会是"#3.jpg#3.gif"?规则和限定都没说清楚, 自然没法看
      

  7.   

    不好意思忘记把规则说清楚了:
    数字是由小到大排列,比如"#1.jpg#2.gif#3.jpg#4.jpg" 或"#1.jpg#3.jpg#4.gif"
    但不可能出现"#3.jpg#3.gif" 这样数字相同的情况
      

  8.   

    public static String combine(String s1, String s2) {
    if (s1 == null || s1.trim().length() == 0) {
    return s2;
    }
    if (s2 == null || s2.trim().length() == 0) {
    return null;
    }
    String[] a1 = s1.split("#");
    String[] a2 = s2.split("#");
    int index2 = 0;
    int index1 = 0;
    StringBuffer result = new StringBuffer();
    for (; index1 < a1.length; index1++) {
    String temp1 = a1[index1];
    if (temp1 == null || temp1.trim().length() == 0) {
    continue;
    }
    if(index2>=a2.length){
    break;
    }
    for (; index2 < a2.length; index2++) {
    String temp2 = a2[index2];
    if (temp2 == null || temp2.trim().length() == 0) {
    continue;
    }
    if (temp1.compareTo(temp2) < 0) {
    result.append("#").append(temp1);
    break;
    } else if (temp1.compareTo(temp2) == 0) {
    result.append("#").append(temp1);
    index2++;
    break;
    } else {
    result.append("#").append(temp2);
    }
    } }
    for (; index1 < a1.length; index1++) {
    result.append("#").append(a1[index1]);
    }
    for (; index2 < a2.length; index2++) {
    result.append("#").append(a2[index2]);
    }
    return result.toString().substring(1);
    }
      

  9.   

    str1="#1.jpg#2.gif#3.gif#4.jpg";
    str2="#1.jpg#3.jpg";
    的结果?