java - 如何在java中按空格和点分割
问题描述
我有很多文字,比如“ J.Depp是美国演员”。我如何在J.Depp 之类的地方拆分“ J. Depp” 我写了一些代码但它不起作用
list.forEach(it -> {
String[] s = it.split(" ");
for (String temp : s) {
if (temp.contains(".")) {
String[] temp2 = temp.split(".");
//
}
}
list2.add(s + " .");
});
解决方案
String.replaceAll()
会成功的
temp.replaceAll("\\.", ". ");
要跳过已经有空格的点,您可以使用非捕获负前瞻:
temp.replaceAll("\\.(?!\\s)", ". ");
另见Pattern
参考。
推荐阅读
- macos - Core Bluetooth 和 IOBluetooth 有什么区别
- javascript - 有没有办法在java脚本中简化这个函数?
- docker - 各种shell中的反斜杠、反引号和插入符号有什么意义
- python - 如何使用具有正拟合值的 statsmodels 线性回归?
- google-chrome-extension - 在 box.com、Onedrive、Google 驱动箱等存储网站上未收到浏览器的 OnBeforePrint 事件
- node.js - mup 部署上的流星构建失败
- kubernetes - 如何在 Kubernetes 上使用 dotnet core agent 来实现弹性 apm?
- python - 在 Rstudio 的 mac 上运行 clinspacy 时无法配置 python
- java - JSP FORM POST:空属性
- javascript - 如何从javascript中的特定类获取数据?