String.split
来源:互联网 发布:淘宝我的店铺打不开 编辑:程序博客网 时间:2024/06/05 14:33
分割字符串碰到了一些疑问,在此整理一下。
1.使用特殊字符“.”或”|“分割时需要转译
1.不使用反斜杠转义,使用“.”进行分割没有结果
/** * result: empty */ public static void splitDotWithoutBackSlash() { String testString = "hello.world"; String[] splitArray = testString.split("."); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.print(" " + s); } } /** * result: hello world */ public static void splitDotWithBackSlash() { String testString = "hello.world"; final String[] splitArray = testString.split("\\."); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.print(" " + s); } }
1.不使用反斜杠转义,使用“|”进行分割会可能会出现奇葩结果
/** * result: h e l l o | w o r l d */ public static void splitDotWithoutBackSlash() { String testString = "hello|world"; String[] splitArray = testString.split("|"); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.print(" " + s); } } /** * result: hello world */ public static void splitDotWithBackSlash() { String testString = "hello|world"; final String[] splitArray = testString.split("\\|"); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.print(" " + s); } }
2.最简单的分割样式
1.将所有符合条件的字符串都进行分解
/** * result: * I * love * my * home */ public static void splitBlank() { String testString = "I love my home"; String[] splitArray = testString.split(" "); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.println(" " + s); } }
2.限制分解的结果集的个数,最后一组会囊括剩余部分
/** * result: * I * love * my home */ public static void splitBlankLimitNum() { String testString = "I love my home"; String[] splitArray = testString.split(" ", 3); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.println(" " + s); } }
3.使用正则表达式,对字符串进行分割
/** * result: I love my home */ public static void splitRegular() { String testString = "I,love.my'home"; String[] splitArray = testString.split("[^a-zA-Z0-9]"); for (int i = 0; i < splitArray.length; i++) { String s = splitArray[i]; System.out.print(" " + s); } }
参考地址
http://www.360doc.com/content/13/0728/19/13247663_303185497.shtml
http://juck.iteye.com/blog/1541247
1 0
- stringutils.split string.split
- String#split
- String split()
- String.split()
- String.split
- String split()
- String.split()
- String.split
- String split
- String .split
- string.split
- String.split
- String split()
- String.split()
- String.split
- String.split()
- String.split
- Split String
- apache所有开源项目文件
- Apple Watch 开发详解
- SmartFoxServer 2X服务器应用开发基础
- cocos2dx源码分析:TrianglesCommand的合并渲染
- PhpStorm 常用快捷键和配置
- String.split
- UIApplication作用
- 15.6 DS1302通信时序介绍
- 15.7 DS1302的BURST模式
- 玩无限消乐的设计示例(基础篇)DataGridView控件动态添加、设置、试载图片
- 15.8 C语言复合数据类型
- 关于Oracle PL\SQL代码的wrapper与unwrapper
- 15.9 单片机电子时钟程序设计
- cocos2dx源码分析:渲染指令TrianglesCommand