我正在尝试使用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新手) 最佳答案 将您的代码替换为 System.out.println(“ befor删除逗号:” + sbf); 字符串sbf1 = sbf.toString()。substring(0,sbf.length()-5); System.out.println(“ sbf1 Rishi --->” + sbf1); 如果(sbf1.trim()。endsWith(“,”)){ sbf1 = sbf1.trim()。substring(0,sbf1.length()-1); sbf1 + =“”; System.out.println(“删除逗号后,字符串为:” + sbf1); }因为您的字符串以结尾。抱歉,午餐时间。所以这只是一个肮脏的把戏。最后编辑:您的问题不是逗号,而是您添加逗号的方式。使用我提供的修改后的代码更改您的代码。 //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); 10-04 17:43