java中怎么样把aa,bb,cc,dd 的字符串变成'aa’,'bb','cc','dd'的字符串,就是以逗号分隔开的每个字符串

加上单引号
userlist="aa,bb,cc,dd";
String result = userlist.replaceAll(",", " ','");
String re = " ' " + result + " ' ";

谢谢大家了!这个是我自己的方法!

public class StringU {

public static void main(String[] args) {

String str = "aa,bb,cc,dd";

String[] ary = str.split(",");

StringBuilder sb = new StringBuilder();

for(int i= 0; i < ary.length; i++){
sb.append("'").append(ary[i]).append("',");
}

sb.deleteCharAt(sb.length()-1);

String newStr = sb.toString();
System.out.println("new string is: " + newStr);
}

}

---------------
new string is: 'aa','bb','cc','dd'

===>方法2, 我觉得这样更快,但比较投机取巧
public class StringU {

public static void main(String[] args) {

String str = "aa,bb,cc,dd";

str = "'" + str.replace(",", "','").concat("'");

System.out.println(str);
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-09-29
public class test {
public static void main(String[] args) {
String str="aa,bb,cc";
String str_temp ="";
int i = 0;
while(i>=0){
int j = i;
i = str.indexOf(",",j+1);
if(j==0)
str_temp +="'"+str.substring(j, i)+"'";
else if(i==-1)
str_temp +=","+"'"+str.substring(j+1,str.length())+"'";
else
str_temp +=","+"'"+str.substring(j+1,i)+"'";
};
System.out.println(str_temp);
}
}
第2个回答  2011-09-29
string.split(',')
第3个回答  2011-09-29
string类中有意哥split方法你可以参考试试
这个方法的作用就是用来分割字符串的
相似回答