【Java学习笔记】使用split()方法分割字符串
2012-12-17 21:18
1006 查看
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
出处:http://www.cnblogs.com/gnuhpc/
// Use split() to extract substrings from a string. class SplitDemo { static void showSplit(String[] strs) { for(String str : strs) System.out.print(str + "|"); System.out.println("/n"); } // Demonstrate split(). public static void main(String args[]) { String result[]; // Split at spaces. String testStr = "This is a test."; System.out.println("Original string: " + testStr); result = testStr.split("//s+"); System.out.print("Split at spaces: "); showSplit(result); // Split on word boundaries. testStr = "One, Two, and Three."; System.out.println("Original string: " + testStr); result = testStr.split("//W+"); System.out.print("Split at word boundaries: "); showSplit(result); // Split same string on commas and zero or more spaces. System.out.println("Original string: " + testStr); result = testStr.split(",//s*"); System.out.print("Split at commas: "); showSplit(result); // Split on word boundaries, but allow embedded // periods and @. testStr = "Jerry Jerry@HerbSchildt.com"; System.out.println("Original string: " + testStr); result = testStr.split("[//W && [^.@]]+"); System.out.print("Allow . and @ to be part of a word: "); showSplit(result); // Split on various punctuation and zero or more trailing spaces. testStr = "This, is. a!:; test?"; System.out.println("Original string: " + testStr); result = testStr.split("[.,!?:;]+//s*"); System.out.print("Split on various punctuation: "); showSplit(result); } } 输出: Original string: This is a test. Split at spaces: This|is|a|test.| Original string: One, Two, and Three. Split at word boundaries: One|Two|and|Three| Original string: One, Two, and Three. Split at commas: One|Two|and Three.| Original string: Jerry Jerry@HerbSchildt.com Allow . and @ to be part of a word: Jerry|Jerry@HerbSchildt.com| Original string: This, is. a!:; test? Split on various punctuation: This|is|a|test|
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
相关文章推荐
- Java字符串分割方法split()的功能以及使用方法的详细介绍!
- Java学习笔记2:使用replaceAll()方法替换字符串中的反斜杠:左斜杠(\)和右斜杠(/)
- 0714学习笔记list常用操作索引(index)和切片(slice) 字符串的方法分割split和连接join
- Java中分割字符串的方法--String.split()
- java 学习笔记(二) 字符串分割
- java学习笔记(四)----对象、数组作为参数传递,静态变量、静态方法的使用,内部类,使用文档注释
- C#中使用split分割字符串的几种方法小结
- java分割字符串split()使用需注意的地方
- Java学习笔记十七:Java中static使用方法
- java学习笔记(面向对象、类的使用、方法的重载)
- java 笔记 split 分割字符串
- java分割字符串split()使用需注意的地方,转义字符使用
- 学习了,使用Java截取字符串的方法
- java反射学习笔记(3)----使用反射来调用方法
- Java学习笔记 50. 三种不能使用方法覆盖的情况
- 【Java学习笔记之六】java三种循环(for,while,do......while)的使用方法及区别
- C#中使用split分割字符串的几种方法
- Java学习笔记2:当构造方法有多个参数时考虑使用Builder
- Java学习笔记43. 使用对象作为方法的参数
- 字符串分割-Java中的String.split()方法