删除最后一个char逗号不起作用

时间:2014-12-19 07:05:16

标签: java

我正在尝试使用java从字符串中删除逗号的最后一个字符。这是我的代码,

//String feature=rs.getString("getvaluesfromDB");
String feature="Games-About Myself,Intro About Myself";
String [] b = feature.split("[\\-:\\;:\\,]");

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

    ResultSet rs2 = st2.executeQuery("select * from videonameslist where videoname='"+b[i]+"'");

    if(rs2.next()) {
        sbf.append("<a href='GameplayfromPlanner.jsp?sno="+b[i]+"' title='Click and Play this Game!' id='"+snoval+"' onclick='getIdforplaygame(this.id)'>").append(b[i]).append(",").append("</a>").append(" ");
    } else {
        sbf.append(b[i]).append(",").append(" ");
    }   

    System.out.println("befor  remove comma :"+sbf1);

    String sbf1 = sbf.toString();  

    if (sbf1.endsWith(",")) {
        sbf1 = sbf1.substring(0, sbf1.length() - 1); 
        System.out.println("after removing comma, the string is :"+sbf1);
    } else {
        System.out.println("Sorry there some proble in removing! :"+sbf1);
    }

其实我正在创建从数据库值中找到数据的href标签。最后我退出的是, 删除逗号之前:<a href='GameplayfromPlanner.jsp?sno=About Myself' title='Click and Play this Game!' id='9' onclick='getIdforplaygame(this.id)'>About Myself,</a> <a href='GameplayfromPlanner.jsp?sno=Intro About Myself' title='Click and Play this Game!' id='9' onclick='getIdforplaygame(this.id)'>Intro About Myself,</a>

或有时输出是这样的, 删除逗号之前:<a href='GameplayfromPlanner.jsp?sno=Five senses' title='Click and Play this Game!' id='8' onclick='getIdforplaygame(this.id)'>Five senses,</a> Sense of touch,

并且条件总是转到其他部分。

埃文,我试过

String sbf1 = sbf.toString(); sbf1 = sbf1.substring(0, sbf1.length() - 1);

if (sbf1.endsWith("</a>")) {
    sbf1 = sbf1.substring(0, sbf1.length() - 2); 
} else {
}

但没有用

我错了,如何解决这个问题? (我是java新手)

2 个答案:

答案 0 :(得分:1)

也许字符串不会在,中结束,也可能在逗号后面有空格,制表符或其他内容。尝试:

if (sbf1.trim().endsWith(",")) {

答案 1 :(得分:1)

用您的代码替换     System.out.println(“befor remove逗号:”+ sbf);             String sbf1 = sbf.toString()。substring(0,sbf.length() - 5);
            System.out.println(“sbf1 Rishi ---&gt;”+ sbf1);             if(sbf1.trim()。endsWith(“,”)){                 sbf1 = sbf1.trim()。substring(0,sbf1.length() - 1);                 sbf1 + = “”;                 System.out.println(“删除逗号后,字符串为:”+ sbf1);             } 因为你的字符串以&lt;结尾/ a&gt;。对不起兄弟,午餐时间。所以只是一个肮脏的把戏。

最终编辑:

你的Problme不是逗号,而是你追加它们的方式。使用我提供的修改代码更改您的代码。

    //String feature=rs.getString("getvaluesfromDB");
    String feature="Games-About Myself,Intro About Myself";
    String [] b = feature.split("[\\-:\\;:\\,]");
    StringBuffer sbf = new StringBuffer();
    for(int i=0;i<b.length;i++) {

        ResultSet rs2 = st2.executeQuery("select * from videonameslist where videoname='"+b[i]+"'");

        if(rs2.next()) {
            if(sbf.length()==0){
                sbf.append("<a href='GameplayfromPlanner.jsp?sno="+b[i]+"' title='Click and Play this Game!' id='id' onclick='getIdforplaygame(this.id)'>").append(b[i]).append("</a>").append(" ");
            }else{
                sbf.append("<a href='GameplayfromPlanner.jsp?sno="+b[i]+"' title='Click and Play this Game!' id='id' onclick='getIdforplaygame(this.id)'>").append(",").append(b[i]).append("</a>").append(" ");
            }
        } else {
            if(sbf.length()==0){
                sbf.append(b[i]).append(" ");
            }else{
                sbf.append(",").append(b[i]).append(" ");
            }
        }  
        /*
         * No Need to remove commas
         * System.out.println("befor  remove comma :"+sbf1);
        String sbf1 = sbf.toString();  
        if (sbf1.endsWith(",")) {
            sbf1 = sbf1.substring(0, sbf1.length() - 1); 
            System.out.println("after removing comma, the string is :"+sbf1);
        } else {
            System.out.println("Sorry there some proble in removing! :"+sbf1);
        }*/
}   
System.out.println("final sbf-->"+sbf);