Java代码
- importjava.util.regex.Pattern;
- publicclassSplitDemo{
- privatestaticfinalStringREGEX=":";
- privatestaticfinalStringINPUT="one:two:three:four:five";
- publicstaticvoidmain(String[]args){
- Patternp=Pattern.compile(REGEX);//将给定的正则表达式编译到模式中
- String[]items=p.split(INPUT);//围绕此模式的匹配拆分给定输入序列。
- for(Strings:items){
- System.out.println(s);
- }
- System.out.println("#############");
- Patternstr=Pattern.compile(":");
- String[]strArray=str.split(INPUT,3);
- for(Strings:strArray){
- System.out.println(s);
- }
- System.out.println("#############");
- String[]strArray2=str.split(INPUT,-3);
- for(Strings:strArray2){
- System.out.println(s);
- }
- }
- }
- 运行结果:
- one
- two
- three
- four
- five
- #############
- one
- two
- three:four:five
- #############
- one
- two
- three
- four
- five