How to split a string with any whitespace chars as delimiters(如何使用任何空白字符作为分隔符拆分字符串)
问题描述
我需要将什么正则表达式模式传递给 java.lang.String.split() 以使用所有空白字符 (' ' 将字符串拆分为子字符串数组>、' '、'
' 等)作为分隔符?
What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (' ', ' ', '
', etc.) as delimiters?
推荐答案
有些东西
myString.split("\s+");
这会将所有空格分组为分隔符.
This groups all white spaces as a delimiter.
所以如果我有字符串:
"Hello[space character][tab character]World"
这应该产生字符串 Hello" 和 World" 并省略 [space] 和[tab].
This should yield the strings "Hello" and "World" and omit the empty space between the [space] and the [tab].
正如 VonC 指出的那样,应该转义反斜杠,因为 Java 会首先尝试将字符串转义为特殊字符,然后发送 that 进行解析.你想要的是文字s",这意味着,你需要传递\s".可能会有点混乱.
As VonC pointed out, the backslash should be escaped, because Java would first try to escape the string to a special character, and send that to be parsed. What you want, is the literal "s", which means, you need to pass "\s". It can get a bit confusing.
\s 等价于 [ \t\n\x0B\f\r].
这篇关于如何使用任何空白字符作为分隔符拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用任何空白字符作为分隔符拆分字符串
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- JPA惰性列表上的流 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
