java split

来源:互联网 发布:智云软件 编辑:程序博客网 时间:2024/06/08 11:34

分隔字符串.返回分隔后的字符串数组

stringObj.split([separator,[limit]])  第一个参数是个正则表达式, 第二个参数可选
 

public static void split拆分字符串(){
  //split  vt.分裂;分开;
  
    String s1 = "hello there";
    String s3 = "this is a test";
   
  String[] s2 =s1.split("e");//返回"h","llo th","r"
  
  //按s分隔成两个 就是按第一个s来分
  //split("s",3); 按s分隔成三个  就是按第一个第二个s来分  返回"thi","i"," a test"
  
  String[] s4 = s3.split("s",2);//返回"thi"和" is a test"
  
  System.out.println("length:"+s2.length);
  for(String s:s2){
   System.out.println(s);
  }
  System.out.println("length:"+s4.length);
  for(String s:s4){
   System.out.println(s);
  }
  System.out.println("--------------------------------------------------------");
  //上面的都简单. 
  

0 0
原创粉丝点击