- public static void method_tran() {
- String s1 = "aSAs fSabcd sdf";
- StringBuilder sb = new StringBuilder();
- Pattern p = Pattern.compile("(\\w+\\b)");
- Pattern p1 = Pattern.compile("(\\w)(\\w*)");
- Matcher m = p.matcher(s1);
- int i=0;
- while (m.find()) {
- String str = m.group().toUpperCase();
- int e = str.length();
- sb.append(str);
- String firstLetter = sb.substring(sb.length()-e, sb.length()-e+1);
- sb.replace(sb.length()-e, sb.length()-e, firstLetter.toLowerCase());
- sb.append(" ");
-
- }
- sb.delete(sb.length()-1, sb.length());
- sop(sb);
- }
- public static void sop(Object obj) {
- System.out.println(obj);
- }
复制代码 这样写比较完美,任何单词都能识别了,
就是先把每个单词取出来,然后把单词大写,再把这个单词的首字母小写,最后存到StringBuilder中
然后打印StringBuilder
|