日期:2014-05-18  浏览次数:20680 次

jsp对字符串操作,小弟在线等
我有一字符传
large_aa.gif#small_aa.gif#large_bb.gif#small_cc.gif#large_cc.gif#large_dd.gpg#small_dd.gif
#small_ee.gif
我需要通过jsp将字符串晒选,最终得到
small_aa.gif#large_bb.gif_s#small_cc.gif#smal_dd.gif这样一个字符串

解释,主要看aa、bb、cc等这样的名字。有large也有small的就只留下small的(如

large_aa.gif#small_aa.gif,只留small_aa.gif)只有large而没有small的就把large的留下(如

large_bb.gif,没有small只好留下large),没有large而只有small的也要不会留下small(如

small_ee.gif也不会被留下)。

请给出程序代码,小弟初学。在线等,谢谢

------解决方案--------------------
String source = "...... ";
String[] ctx = source.split( "# ");
List listSmall = new ArrayList();
List listLarge = new ArrayList();
List listBoth = new ArrayList();
for (int i = 0; i < ctx.length; i ++){
String suffix = ctx[i].substring(5);
if (ctx[i].startWith( "large ")){
if(listSmall.contains(suffix)){
listSmall.remove(suffix);
listBoth.add(suffix);
}else{
listLarge.add(suffix);
}
}else{
if(listLarge.contains(suffix)){
listLarge.remove(suffix);
listBoth.add(suffix);
}else{
listSmall.add(suffix);
}
}
}
String result = " ";
for (int i = 0; i < listBoth.size(); i++){
result += (String)listBoth.get(i) + "# ";
}
for (int i = 0; i < listLarge.size(); i++){
result += "large "+(String)listLarge.get(i) + "# ";
}
if (result.endWith( "# ")){
result = result.substring(0,result.length()-2);
}
------解决方案--------------------
public static String subStr(String args) {
String[] a = args.split( "# ");
String total = " ";
List seLarge = new ArrayList();
List seSmall = new ArrayList();
List seSmallother = new ArrayList();
for (int i = 0; i < a.length; i++) {
if (a[i].startsWith( "large "))
seLarge.add(a[i]);
else if (a[i].startsWith( "small ")) {
seSmall.add(a[i]);
seSmallother.add(a[i].substring(a[i].indexOf( "_ ") + 1, a[i]
.indexOf( ". ")));
}

}
if (seLarge.size() > 1) {
for (int i = 0; i < seLarge.size(); i++) {
String nodeLarge = seLarge.get(i).toString();
nodeLarge = nodeLarge.substring(nodeLarge.indexOf( "_ ") + 1,
nodeLarge.lastIndexOf( ". "));
/* 根据(aa,bb)关键字 判断是否 存在 small*/
if (Collections.binarySearch(seSmallother, nodeLarge) > -1) {
total += seSmall.get(Collections.binarySearch(seSmallother,
nodeLarge))
+ "# ";
} else {
total += seLarge.get(i) + "# ";
}
}
}
return total;
}