Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中拆分带问号、点和感叹号的字符串_Java_Arrays_String_Split - Fatal编程技术网

如何在java中拆分带问号、点和感叹号的字符串

如何在java中拆分带问号、点和感叹号的字符串,java,arrays,string,split,Java,Arrays,String,Split,如何使用分隔符作为问号、点和感叹号将其拆分为字符串数组。然后在每个大写字符前加一个空格。之后,将该字符设置为小写 String one = "ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit?"; 成功了 String one = "ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit?"

如何使用分隔符作为问号、点和感叹号将其拆分为字符串数组。然后在每个大写字符前加一个空格。之后,将该字符设置为小写

String one = "ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit?";
成功了

    String one = "ATrueRebelYouAre!EveryoneWasImpressed.You'llDoWellToContinueInTheSameSpirit?";

    String oneWithSpaces = one.replace("!","! ").replace("?","? ").replace(".",". ");
    String[] splitedWithDelimeters = oneWithSpaces.split(" ");


    for (int j = 0; j < splitedWithDelimeters.length; j++) {

        String[] str = splitedWithDelimeters[j].split("(?=[A-Z])");



        for (int i = 0; i < str.length; i++) {
            if (i == 0) {
                System.out.print(str[i] + " ");
            } else if (i > 0) {
                System.out.print(str[i].toLowerCase() + " ");
            }
        }
    }
String one=“aturebelyouare!每个人都已按下。您是否愿意继续进行Messpirit?”;
字符串oneWithSpaces=1.replace(“!”,“!”).replace(“?”,“?”).replace(“.”,”);
字符串[]splitedWithDelimeters=oneWithSpaces.split(“”);
对于(int j=0;j0),则为else{
System.out.print(str[i].toLowerCase()+“”);
}
}
}

对于使用多个分隔符进行拆分,可以使用正则表达式或运算符:

String[] tokens = one.split("\\?|\\.|\\!");
要在每个大写字母前加空格,请执行以下操作:

String two = "";
for (int i = 0; i < tokens.length; i++) {
    String phrase = tokens[i];
    String outputString = "";
    for (int k = 0; k < phrase.length(); k++) {
        char c = phrase.charAt(k);
        outputString += Character.isUpperCase(c) ? " " + c : c;
    }
two += outputString + " ";
}
two = two.replaceAll("\\s+", " ");
但你也可以这样做:

String two = "";
for (int k = 0; k < one.length(); k++) {
    char c = one.charAt(k);
    two += Character.isUpperCase(c) ? " " + c : c;
}
two = two.replaceAll("\\s+", " ");
System.out.println(two);

使用。哪里?=是积极的前瞻,你试过什么了吗?这听起来像是我的任务。我试了两天。我已经厌倦了。我尝试了用正则表达式吐痰,我尝试了替换方法,我尝试了将字符串转换为字符数组。至少,编辑您的问题并显示一个包含所有案例的输入示例,以及预期的输出。非常感谢。还有,在这之后如何使除第一个字母以外的所有字母都小写?您可以像回答一样,或者将所有字母都设置为小写,然后找到分隔符的索引,并将它们后面的第一个字母改为大写
String two = "";
for (int k = 0; k < one.length(); k++) {
    char c = one.charAt(k);
    two += Character.isUpperCase(c) ? " " + c : c;
}
two = two.replaceAll("\\s+", " ");
System.out.println(two);
A True Rebel You Are! Everyone Was Impressed. You'll Do Well To Continue In The Same Spirit?
    List<String> collect = Arrays.stream(one.split("(?<=[?.!])"))
    .map(sentence -> sentence.replaceAll("(?=[A-Z])", " ").trim() )
    .collect(Collectors.toList());
[A True Rebel You Are!, Everyone Was Impressed., You'll Do Well To Continue In The Same Spirit?]