java 字符串分割函数

标签 java string split

public String\[\] split(String regex)的java文档中

我们可以找到示例:

The string "boo:and:foo", for example, yields the following results with these expressions

Regex          Result

o                   { "b", "", ":and:f" }

第二个结果""来自哪里?来自 boo 之间,还是 oo:and:f 之间?

最佳答案

当您使用分隔符o并且有两个相邻的分隔符时,它会产生一个空字符串。例如,如果您有一个字符串 foo::bar 并将其与 : 分割,您将得到 {"foo", "", "bar"} 因为 :: 之间的空格在分割时被视为空字符串。

如果您想在不包含任何空字符串的情况下进行拆分,请查看 this question .

结束空字符串?

When split is given zero or no limit argument it discards trailing empty fields

引用号:Java: String split(): I want it to include the empty strings at the end

关于java 字符串分割函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119032/

相关文章:

java - Spring MVC Controller 未被调用

python - 将代码点的 numpy 数组与字符串相互转换

python - 将文件名拆分为part1和part2并创建目录part1并将文件part2放入part1目录的脚本

python - 分割一串数字

python - 拆分具有固定宽度的 *.dat 文件,每个宽度已知

Java非阻塞套接字写入

java - 相同的查询在 MSSQL 和 Hibernate 3 中给出不同的输出

java - 401 使用Jsoup连接HTTPS出错?

javascript - 是否可以使用字符串从对象调用方法?

Java Reflection API - 获取 String[] 字段的值