字符串“I am a Chinese and I love my country”,请将and之前的内容和之后的内容转化为大写字母, 并将转换结果分别输出。

字符串“I am a Chinese and I love my country”,请将and之前的内容和之后的内容转化为大写字母, 并将转换结果分别输出。

代码如下:

public class Main {
    public static void main(String[] args) {
        String text = "I am a Chinese and I love my country";
        int index = text.indexOf("and");
        String upperText1 = text.substring(0, index).toUpperCase();
        String upperText2 = text.substring(index + 3).toUpperCase();
        System.out.println(upperText1 + "AND" + upperText2);
        System.out.println(upperText1);
        System.out.println(upperText2);
    }
}

运行结果:

I AM A CHINESEAND I LOVE MY COUNTRY
I AM A CHINESE
I LOVE MY COUNTRY

【以下回答由 GPT 生成】

public class StringManipulation {
    public static void main(String[] args) {
        String str = "I am a Chinese and I love my country";

        // Find the index of "and"
        int index = str.indexOf("and");

        // Extract the substring before "and"
        String beforeAnd = str.substring(0, index).toUpperCase();

        // Extract the substring after "and"
        String afterAnd = str.substring(index + 3).toUpperCase();

        // Print the results
        System.out.println(afterAnd);
        System.out.println(beforeAnd);
    }
}

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^