比如,HelloWorldToAll 分割成 Hello World To All谢谢!

解决方案 »

  1.   


    public class keyboard3 {    public static void main(String[] arge) throws Exception {
            String s = "HelloWorldToAll";
            StringBuilder sb = new StringBuilder();
            char[] c = s.toCharArray();
            int start = 0;
            int end = 0;
            for (int i = 0; i < c.length; i++) {
                if (c[i] < 'a' && i != 0) {
                    start = end;
                    end = i;
                    sb.append(s.substring(start,end) + " ");
                }
            }
            sb.append(s.substring( end,c.length));
            System.out.println(sb.toString());
        }
    }
      

  2.   


    // 作为一个方法更好!
    public class keyboard3 {    public static void main(String[] arge) throws Exception {
            String s = "helloworldtoall";
            System.out.println(toSpace(s));
        }    static String toSpace(String s) {
            if (!s.matches("\\w*[A-Z]\\w*"))
                return s;
            StringBuilder sb = new StringBuilder();
            char[] c = s.toCharArray();
            int start = 0;
            int end = 0;
            for (int i = 0; i < c.length; i++) {
                if (c[i] < 'a' && i != 0) {
                    start = end;
                    end = i;
                    sb.append(s.substring(start,end) + " ");
                }
            }
            sb.append(s.substring(end,c.length));
            return sb.toString();
        }
    }
      

  3.   

    //上面的错了表达式,写着急了
    public class keyboard3 {    public static void main(String[] arge) throws Exception {
            String s = "HelloW orldTo AlA";
            System.out.println(toSpace(s));
        }    static String toSpace(String s) {
            if (!s.matches(".*?[A-Z].*?"))
                return s;
            StringBuilder sb = new StringBuilder();
            char[] c = s.toCharArray();
            int start = 0;
            int end = 0;
            for (int i = 0; i < c.length; i++) {
                if ('A' <= c[i] && c[i] <= 'Z' && i != 0) {
                    start = end;
                    end = i;
                    if (String.valueOf(c[i - 1]).equals(" ")) {
                        sb.append(s.substring( start,end));
                    } else {
                        sb.append(s.substring( start,end) + " ");
                    }
                }
            }
            sb.append(s.substring(end,c.length));
            return sb.toString();
        }
    }
      

  4.   

    这个其实可以很简单啊System.out.println(Arrays.toString("HelloWorldToAll".split("(?<=[a-z])(?=[A-Z])")));
    //[Hello, World, To, All]