Home > Java > javaTutorial > body text

Java Example - String Splitting

黄舟
Release: 2017-02-22 09:43:21
Original
1503 people have browsed it

以下实例使用了 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("");
      }
   }}
Copy after login

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

www

runoob

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

runoob

com
Copy after login

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




source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template