日期:2014-05-20 浏览次数:20738 次
public static void main(String[] args) { String s = "accpHelloWorldHH"; String[] ss = s.split("(?=[A-Z])"); for(int i = 0 ;i < ss.length; i ++){ System.out.println(ss[i]); } }
------解决方案--------------------
我把 1 楼的代改进了一下,仅供参考:
public class RegexTest { public static void main(String[] args) { String s = "AAccpHelloWorldHH"; String[] ss = s.split("(?<!^)(?=[A-Z])"); for(int i = 0 ;i < ss.length; i ++){ System.out.println(ss[i]); } } }
------解决方案--------------------
我不会正则表达式,所以就写了一个不用正则的,你可以看看。我也是菜鸟,向各位高手学习正则的写法
public class StringSplit { public static void main(String[] args) { String s = "accpHelloWorldHH"; char[] c=s.toCharArray(); int i=0; StringBuilder sb = new StringBuilder(); while(i<s.length()){ while(i<s.length()&&c[i]>'Z') { sb.append(c[i]); i++; } System.out.print(sb.toString()+" "); if(i<s.length()){ sb = new StringBuilder(); sb.append(c[i++]); } } System.out.println(sb.toString()); } }