我是 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();
最佳答案
This应该让你开始......
一个例子...
int spaceIndex = loveHate.indexOf(" "); //index of a first space character
String first = loveHate.substring(0, spaceIndex);
String rest = loveHate.substring(spaceIndex + 1);
String reversed = rest + " " + first;
关于字符串操作中的Java字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611554/