两个字符串合并为一个字符串的问题 有两个字符串:str1="#1.jpg#2.gif#3.jpg#4.jpg";str2="#1.jpg#3.gif";合并后的字符串为:1.jpg#2.gif#3.gif#4.jpg有没有简单点的方法,我自己写了好几个循环 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的是什么意思,你是说str1是怎么合并出来的吗 简单的方法就是按规则拆开,然后放到HASHTABLE里面,让重名的自动覆盖,然后再取出来,这个办法不必费脑筋考虑算法。 3.jpg 和 3.gif 只出一个? 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); 感谢 shan1119(大天使,卐~解!) 的帮忙,只是并不是我要的答案,不是简单把两个字符串合并,str2中有3.gif, str1中也有3.jpg 数字相同但不是同一文件,所以要替换掉str1中的3.jpg为3.gif。 bigc2001(大C) 说的HASHTABLE中可让重名的自动覆盖,能否详细一些,小弟多谢各位了 str1和str2里面的数字是顺序的还是无序的? 如果str1="#1.jpg#2.gif#3.jpg#4.jpg";str2="#3.jpg#1.gif";应该合并出什么来? 还有str2会不会是"#3.jpg#3.gif"?规则和限定都没说清楚, 自然没法看 不好意思忘记把规则说清楚了:数字是由小到大排列,比如"#1.jpg#2.gif#3.jpg#4.jpg" 或"#1.jpg#3.jpg#4.gif"但不可能出现"#3.jpg#3.gif" 这样数字相同的情况 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); } str1="#1.jpg#2.gif#3.gif#4.jpg";str2="#1.jpg#3.jpg";的结果? 二进制字符转换问题 再问public protected 和 private 大学专业发展方向? 求孙卫琴的<tomcat与java web开发技术详解> 源码 这句话什么意思? java包与导入的问题分析,请指导? 程序输出结果不对啊 那里有免费下载教JAVA的课堂录音 java 构造器问题!! 求一个使用tcp协议传送类似数据库表的二维数组的例子。每条记录上的字段数据类型不一定一样。 一个Collections对象的问题 各位在使用myeclipse开发hibernate时,对ORACLE的主键是怎么控制 的
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);
如果str1="#1.jpg#2.gif#3.jpg#4.jpg";
str2="#3.jpg#1.gif";
应该合并出什么来? 还有str2会不会是"#3.jpg#3.gif"?规则和限定都没说清楚, 自然没法看
数字是由小到大排列,比如"#1.jpg#2.gif#3.jpg#4.jpg" 或"#1.jpg#3.jpg#4.gif"
但不可能出现"#3.jpg#3.gif" 这样数字相同的情况
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);
}
str2="#1.jpg#3.jpg";
的结果?