> JAVA中文手册 > Java 实例 - 字符串分割

以下实例使用了 split(string) 方法通过指定分隔符将字符串分割为数组:

//JavaStringSplitEmp.java 文件

public class JavaStringSplitEmp{
   public static void main(String args[]){
      String str = "jan-feb-march";
      String[] temp;
      String delimeter = "-";
      temp = str.split(delimeter);
      for(int i =0; i < temp.length ; i++){
         System.out.println(temp[i]);
         System.out.println("");
         str = "jan.feb.march";
         delimeter = "\\.";
         temp = str.split(delimeter);
      }
      for(int i =0; i < temp.length ; i++){
         System.out.println(temp[i]);
         System.out.println("");
         temp = str.split(delimeter,2);
         for(int j =0; j < temp.length ; j++){
            System.out.println(temp[i]);
         }
      }
   }
}

以上代码实例输出结果为:

jan

feb

march

jan

jan
jan
feb.march

feb.march
feb.march