其实使用的就是String类中的split方法。
public class Test { public static void main(String[] args) { functionDemo(); } public static void functionDemo() { String str = "zhangsan xiao zhaoliu"; String[] names = str.split(" ");//用空格来切时候,如果有很多空格,切出来的效果就不好了 for(String name : names){ System.out.println(name); } } }
public class Test { public static void main(String[] args) { functionDemo(); } public static void functionDemo() { String str = "zhangsan xiaoqiang zhaoliu"; String[] names = str.split(" +");//表示多个空格 for(String name : names){ System.out.println(name); } } }
public class Test { public static void main(String[] args) { functionDemo(); } public static void functionDemo() { String str = "zhangsan.xiaoqiang.zhaoliu"; String[] names = str.split("\."); for(String name : names){ System.out.println(name); } } }
组:((A)(B(C)))
public class Test { public static void main(String[] args) { functionDemo(); } public static void functionDemo() { String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu"; String[] names = str.split("(.)\1+");//加上括号进行封装,有编号 for(String name : names){ System.out.println(name); } } }