黑马程序员技术交流社区

标题: 请教一下String的split方法该怎么用? [打印本页]

作者: paozhuanyinyu    时间: 2014-8-5 23:47
标题: 请教一下String的split方法该怎么用?
本帖最后由 paozhuanyinyu 于 2014-8-8 12:13 编辑

下面是API例子,我不明白以o为定界正则表达式时得出的结果中间为什么有一个空字符串public String[] split(String regex)
Regex结果
:
{ "boo", "and", "foo" }
o
{ "b", "", ":and:f" }


作者: fantacyleo    时间: 2014-8-6 00:03
看API文档中的这句话“The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string”。

"boo:and:foo",boo的两个o之间没有其他字符,因此在第1个后之后被第二个o所终结的字符串就是空字符串""
作者: ssy1939    时间: 2014-8-6 00:49
底层貌似是先将字符串转换为字符数组,然后对数组遍历,根据所给正则表达式,对相应数据进行判断后添加到新数组中,再返回新的数组,不知道对不对
作者: paozhuanyinyu    时间: 2014-8-8 12:12
fantacyleo 发表于 2014-8-6 00:03
看API文档中的这句话“The array returned by this method contains each substring of this string that i ...

谢谢,长知识了!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2