java.util.regex.Pattern类使用举例

  
  
  
  
  1. import java.util.regex.Pattern;  
  2.  
  3. public class SplitDemo {  
  4.  
  5. private static final String REGEX = ":";  
  6. private static final String INPUT = "one:two:three:four:five";  
  7.  
  8. public static void main(String[] args) {  
  9. Pattern p = Pattern.compile(REGEX); //将给定的正则表达式编译到模式中  
  10. String[] items = p.split(INPUT); //围绕此模式的匹配拆分给定输入序列。  
  11. for(String s : items) {  
  12. System.out.println(s);  
  13. }  
  14.  
  15. System.out.println("#############");  
  16.  
  17. Pattern str=Pattern.compile(":");  
  18. String[] strArray=str.split(INPUT,3 );  
  19. for(String s:strArray){  
  20. System.out.println(s);  
  21. }  
  22.  
  23. System.out.println("#############");  
  24.  
  25. String[] strArray2=str.split(INPUT,-3 );  
  26. for(String s:strArray2){  
  27. System.out.println(s);  
  28. }  
  29. }  

运行结果:
one
two
three
four
five
#############
one
two
three:four:five
#############
one
two
three
four
five
 

你可能感兴趣的:(类,使用,举例)