java - why does java's String split("\\s") have an empty string in the array?
问题描述
Given the sentence "The brown fox jumped the fence jumped. The jumped fox? The fox. Jumped"
splitting by whitespace shows an empty string in the array. I expected words or words with punctuation. Where did this empty string come from in the sentence?
解决方案
这是因为您的字符串包含连续的空白字符,例如
jumped. The
^^
这两个空格之间有一个长度为零的字符串,所以如果你拆分每个空格字符,你会得到它们之间的空字符串。
如果您想将它们匹配为单个分隔符,请使用量词,例如
"\\s+"
推荐阅读
- javascript - 转到随机帧,不包括当前帧
- azure-cognitive-search - 如何将索引参数“分析器”值设置为“English.Microsoft”
- regex - 正则表达式将不需要的花括号添加到匹配的字符串
- angular - svg:marker 箭头并不总是显示
- solr - Spring 表达式语言尝试在空上下文对象上调用方法 getTime()
- node.js - 为什么级别 ERROR 没有添加到错误文件中?
- oracle - Oracle 归档日志规范与存储库中的任何归档日志都不匹配
- oracle - 发送显式 CURSOR 作为过程参数
- excel - 为什么当我激活工作表时代码会起作用,而当我不激活时代码会起作用?
- mysql - 如何连接来自不同数据库的2个表不等于