简体   繁体   English

Java 反句不标点

[英]Java Reverse a sentence but not punctuation

Input: My name is Bob.输入:我的名字是鲍勃。

Output: Bob is name My. Output:鲍勃是我的名字。

I have seen plenty of examples on how to reverse each word and then word sequence in a sentence.我已经看到很多关于如何在一个句子中反转每个单词然后单词序列的例子。 However, I do not think I have seen one that I want-example above.但是,我认为我没有看到我想要的例子——上面的例子。 This forum was not helpful, because it only focuses on double quote on start and end of a sentence: How to reverse words in a string but keep punctuation in the right place?这个论坛没有帮助,因为它只关注句子开头和结尾的双引号: 如何反转字符串中的单词但将标点符号保留在正确的位置?

What I tried我试过的

public void rev2(String str) {
    String[] arrStr = str.split(" ");
    for (int i = arrStr.length - 1; i >= 0; i--) {
        if (!arrStr[i].contains(".")) {
            System.out.print(arrStr[i] + " ");
        }
    }
}

public static void main(String[] args) {
    Test t = new Test();
    t.rev2("My name is Bob.");
}

The code above does not work as expected.上面的代码没有按预期工作。 I probably can convert each string to char and use Character.isAlphabet() or may be use pattern?我可能可以将每个字符串转换为 char 并使用 Character.isAlphabet() 或者可以使用模式?

I could use some ideas.我可以使用一些想法。 Thanks in advance for your time and help.提前感谢您的时间和帮助。

Here is a solution that works within the constraints of the question (some of which are assumed):这是一个在问题约束范围内工作的解决方案(其中一些是假设的):

public void rev2(String str) {
    String[] arrStr = str.split(" ");
    for (int i = arrStr.length - 1; i >= 0; i--) {
        boolean punct = i <= 0 || arrStr[i - 1].contains(".");
        if (!arrStr[i].contains(".")) {
            System.out.print(arrStr[i]);
        } else {
            System.out.print(arrStr[i].substring(0, arrStr[i].length() - 1));
        }
        System.out.print((punct ? ". " : " "));
    }
}

public static void main(String[] args) {
    Tester t = new Tester();
    t.rev2("My name is Bob. I am happy.");
}

Explanation:解释:

The boolean punct looks ahead to see if the next element contains a ".". boolean punct 向前看,看下一个元素是否包含“.”。 If so, we are on a sentence boundry and so added a.如果是这样,我们就在一个句子的边界上,所以添加了一个。 after the current word.在当前单词之后。 If not, we just put a space.如果没有,我们就放一个空格。 We also add a "."我们还添加了一个“。” if we are on the last element in the array.如果我们在数组的最后一个元素上。

Output: happy am I. Bob is name My. Output: happy am I. Bob is name My.

If all you want to do regarding punctuation is keep the period at the end of the sentence, you can print the words followed by a space or by the period depending on whether they are the first word or not, and you can remove the period from the last word by calling substring() .如果您只想在句末保留句号,则可以打印后跟空格或句号的单词,具体取决于它们是否是第一个单词,您可以从最后一个词通过调用substring() Something like this:像这样的东西:

public class Test {
    public void rev2(String str) {
        String[] arrStr = str.split(" ");
        for (int i = arrStr.length - 1; i >= 0; i--) {
            if (!arrStr[i].contains(".")) {
                System.out.print(arrStr[i] + (i == 0 ? "." : " "));
            } else {
                System.out.print(arrStr[i].substring(0, arrStr[i].length() - 1) + " ");
            }
        }
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.rev2("My name is Bob.");
    }
}

Output: Bob is name My. Output:鲍勃是我的名字。

If you want a less verbose code:如果你想要一个不那么冗长的代码:

var list = Arrays.asList("My name is Bob".split(" "));
Collections.reverse(list);
list.stream().map(str -> str.concat(" ")).forEach(System.out::print);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM