Home  >  Article  >  Java  >  Java Example - String Splitting

Java Example - String Splitting

黄舟
黄舟Original
2017-02-22 09:43:211473browse

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

//JavaStringSplitEmp.java 文件public class JavaStringSplitEmp{
   public static void main(String args[]){
      
      String str = "www-runoob-com";
      String[] temp;
      String delimeter = "-";  // 指定分割字符
      temp = str.split(delimeter); // 分割字符串
      // 普通 for 循环
      for(int i =0; i < temp.length ; i++){
         System.out.println(temp[i]);
         System.out.println("");
      }

      System.out.println("------java for each循环输出的方法-----");
      String str1 = "www.runoob.com";
      String[] temp1;
      String delimeter1 = "\\.";  // 指定分割字符, . 号需要转义
      temp1 = str1.split(delimeter1); // 分割字符串
      for(String x :  temp1){
         System.out.println(x);
         System.out.println("");
      }
   }}

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

www

runoob

com------java for each循环输出的方法-----www

runoob

com

以上就是Java 实例 - 字符串分割的内容,更多相关内容请关注PHP中文网(www.php.cn)!




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn