Java 实例 - 字符串分割

Java 教程 · 2019-02-10 07:56:50

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

JavaStringSplitEmp.java 文件

public class JavaStringSplitEmp { public static void main(String args[]){ String str = "www-codercto-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.codercto.com"; String[] temp1; String delimeter1 = "\\."; // 指定分割字符, . 号需要转义 temp1 = str1.split(delimeter1); // 分割字符串 for(String x : temp1){ System.out.println(x); System.out.println(""); } } }

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

www

codercto

com

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

codercto

com

点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html

查看所有标签

BSD Hacks

BSD Hacks

Dru Lavigne / O'Reilly Media, Inc. / 2004-05-24 / USD 24.95

If you want more than your average BSD user--you want to explore and experiment, unearth shortcuts, create useful tools, and come up with fun things to try on your own--BSD Hacks is a must-have. This ......一起来看看 《BSD Hacks》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具