首页 > 解决方案 > 如何在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 + " .");
    });

标签: javastring

解决方案


String.replaceAll()会成功的

temp.replaceAll("\\.", ". ");

要跳过已经有空格的点,您可以使用非捕获负前瞻:

temp.replaceAll("\\.(?!\\s)", ". ");

另见Pattern参考。


推荐阅读