Java 是否有任何方法可以反转保留位置的单词';s案例

Java 是否有任何方法可以反转保留位置的单词';s案例,java,string,Java,String,我需要一个函数,它接受字符串作为输入,并使用保留位置的大小写反转单词 示例: OlLeh dlRow NEb 输入:“你好,本” 输出:“olLeh dlRow NEb”我们将原始句子拆分为单词,并使用StringBuilder@reverse. 然后,我们创建一个字符数组,表示原始单词和反向单词。然后,我们循环遍历所有字符,并通过检查大小写来确保大小写正确 字符串反转(字符串句子){ char[]characters=句子.toCharArray(); 字符串反转=Stream.of(句子分

我需要一个函数,它接受字符串作为输入,并使用保留位置的大小写反转单词

示例:

OlLeh dlRow NEb
输入:“你好,本”


输出:“olLeh dlRow NEb”

我们将原始句子拆分为单词,并使用StringBuilder@reverse. 然后,我们创建一个字符数组,表示原始单词和反向单词。然后,我们循环遍历所有字符,并通过检查大小写来确保大小写正确

字符串反转(字符串句子){
char[]characters=句子.toCharArray();
字符串反转=Stream.of(句子分割(“”)
.map(word->new StringBuilder(word).reverse())
.collect(收集器。连接(“”);
char[]reversedCharacters=reversed.tocharray();
对于(int index=0;index
我们把原来的句子分成几个单词,然后用StringBuilder@reverse. 然后,我们创建一个字符数组,表示原始单词和反向单词。然后,我们循环遍历所有字符,并通过检查大小写来确保大小写正确

字符串反转(字符串句子){
char[]characters=句子.toCharArray();
字符串反转=Stream.of(句子分割(“”)
.map(word->new StringBuilder(word).reverse())
.collect(收集器。连接(“”);
char[]reversedCharacters=reversed.tocharray();
对于(int index=0;index
按以下步骤操作:

public class Main {
    public static void main(String[] args) {
        System.out.println(reverseWordsRetainingPositionCase("HeLlo woRld BEn"));
    }

    static String reverseWordsRetainingPositionCase(String str) {
        String[] words = str.split("\\s+");// Split the string on space(s)
        StringBuilder sb = new StringBuilder(str);// Create a StringBuilder instance with the content of str
        for (String word : words) {
            // Find the word in sb and replace it with its reverse
            sb.replace(sb.indexOf(word), sb.indexOf(word) + word.length(), reverseCharsRetainingPositionCase(word));
        }
        return sb.toString();
    }

    static String reverseCharsRetainingPositionCase(String word) {
        StringBuilder reversedWord = new StringBuilder(word).reverse();// Reverse the word
        for (int i = 0; i < word.length(); i++) {
            // If the character at i in the word is in upper case, change the case of the
            // character at i in the reversed word to upper case. Process the reversed word
            // in the same way for lower case.
            if (Character.isUpperCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toUpperCase(reversedWord.charAt(i)));
            } else if (Character.isLowerCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toLowerCase(reversedWord.charAt(i)));
            }
        }
        return reversedWord.toString();
    }
}
我已经在代码中添加了足够多的注释,以便于理解。如有任何疑问/问题,请随时发表意见。

请按以下步骤进行:

public class Main {
    public static void main(String[] args) {
        System.out.println(reverseWordsRetainingPositionCase("HeLlo woRld BEn"));
    }

    static String reverseWordsRetainingPositionCase(String str) {
        String[] words = str.split("\\s+");// Split the string on space(s)
        StringBuilder sb = new StringBuilder(str);// Create a StringBuilder instance with the content of str
        for (String word : words) {
            // Find the word in sb and replace it with its reverse
            sb.replace(sb.indexOf(word), sb.indexOf(word) + word.length(), reverseCharsRetainingPositionCase(word));
        }
        return sb.toString();
    }

    static String reverseCharsRetainingPositionCase(String word) {
        StringBuilder reversedWord = new StringBuilder(word).reverse();// Reverse the word
        for (int i = 0; i < word.length(); i++) {
            // If the character at i in the word is in upper case, change the case of the
            // character at i in the reversed word to upper case. Process the reversed word
            // in the same way for lower case.
            if (Character.isUpperCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toUpperCase(reversedWord.charAt(i)));
            } else if (Character.isLowerCase(word.charAt(i))) {
                reversedWord.setCharAt(i, Character.toLowerCase(reversedWord.charAt(i)));
            }
        }
        return reversedWord.toString();
    }
}

我已经在代码中添加了足够多的注释,以便于理解。如果有任何疑问/问题,请随时发表评论。

这不是一种直接的方法,但您可以拆分并执行。为什么“olLeh”不以大写字母开头?您可以显示您尝试过的代码吗?
Character
类有
isUpperCase
isLowerCase
toUpperCase
toLowerCase
方法,它们将是您在这里最好的朋友。“O”应该是“OlLeh”中的大写字母。你的例子是错误的。不是一个直接的方法,但你可以拆分,为什么“olLeh”不以大写字母开头?你能展示你尝试过的代码吗?
字符
类有
isUpperCase
isLowerCase
toUpperCase
toLowerCase
方法,它们将是你最好的朋友应该是大写的“OlLeh”。你的例子是错误的。