我是Java新手,想知道如何切換字符串中的單詞。在我的程序中,用戶將文本輸入到字符串中並保存。程序應該將第一個單詞移到字符串的末尾。防爆。 「我喜歡貓和討厭的狗」應該改爲「愛貓和討厭的狗」。字符串操作中的Java字
Scanner in = new Scanner(System.in);
System.out.println("Please enter at least one thing you love and one thing you hate using the words hate and love: ");
String loveHate = in.nextLine();
您可以將字符串拆分爲空格分隔符,然後將數組中的第一個元素彈出到結尾。 – Vulcan