String与StringBuilder相互转换
1. StringBuilder转为String
StringBuilder sb = new StringBuilder();
sb.append("hello");
String s = sb.toString();
2. String转为StringBuilder
//string转换为stringbuilder,通过构造方法实现,
String s1 = "world";
StringBuilder sb1 = new StringBuilder(s);
获取字符串中第一个中文汉字
for (int index = 0;index<=s.length()-1;index++){
//将字符串拆开bai成单个的字符
String w=s.substring(index, index+1);
if(w.compareTo("\u4e00")>0&&w.compareTo("\u9fa5")<0){// \u4e00-\u9fa5 中文汉字的范围
System.out.println("第一个中文的索引位置:"+index+",值是:"+w);
break;
}
}
三元判断:
- 判断是先生还是女士
String sex = sexCode.equals(SysCode.SEX.MAN) ? "先生" : "女士";
标签:中文,转换,String,StringBuilder,字符串,sb
From: https://www.cnblogs.com/prosperous-ending-0925/p/16598798.html