如何反转字符串中的单词但将标点符号保持在正确的位置?
How to reverse words in a string but keep punctuation in the right place?
我编写了以下代码来反转输入字符串:
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] words = sentence.split(" ");
String reversedSentence = "";
for(int i = words.length - 1; i >= 0 ; i--)
{
reversedSentence += words[i] + " ";
}
System.out.println(reversedSentence);
但是它没有给我想要的结果。我需要标点符号成为它所附单词的一部分,但仍要切换到单词的右侧。例如,如果您输入
“The quick brown fox jumps over the lazy dog”
我希望输出为
“dog lazy the over jumps fox brown quick The”
我实际得到的是:
dog” lazy the over jumps fox brown quick “The
如果只想处理输入首尾的双引号,只需将子串反转,稍后再添加即可。例如
if (sentence.startsWith("\"") && sentence.endsWith("\"")) {
sentence = sentence.substring(1, sentence.length()-1);
}
最后在拆分、反转和连接打印之后:
System.out.println('"' + reversedSentence + '"');
还有 2 条建议:
1) 您的 for 循环留下尾随 space。最后一个字不要加space
2) 你应该使用 StringBuilder
来连接字符串。例如
StringBuilder reversedSentence = new StringBuilder();
for (int i = words.length - 1; i > 0; i--) {
reversedSentence.append(words[i]).append(' ');
}
reversedSentence.append(words[0]);
System.out.println('"' + reversedSentence.toString() + '"');
将字符串分解为数组并使用Collections.reverse()如下:
public class StringReverse {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] array = sentence.replaceAll("\“|”", "").split(" ");
StringBuilder sb = new StringBuilder();
List<String> list = Arrays.asList(array);
Collections.reverse(list);
array = (String[]) list.toArray();
for (int i = 0; i < array.length; i++) {
sb.append(array[i]).append(" ");
}
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
}
用Java8Arrays.stream,就这么简单:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
public class StringReverse {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
StringBuilder sb = new StringBuilder();
Arrays.stream(sentence.replaceAll("\“|”", "").split(" "))
.collect(Collectors.toCollection(ArrayDeque::new))
.descendingIterator()
.forEachRemaining(e -> sb.append(e).append(" "));
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
如果标点符号也打开和关闭。就像你的例子一样。你可以使用这样的东西:
很脏。我稍后会编辑它。我做的不多java。
String[][] punctuations = new String[][] {
{"(", ")"},
{"“", "”"}
};
for (String[] punctuation : punctuations) {
if (sentence.contains(punctuation[0])) {
int index_0 = sentence.indexOf(punctuation[0]);
int index_of_next_space = sentence.indexOf(" ", index_0);
String old_string_0 = sentence.substring(index_0, index_of_next_space);
String new_string_0 = old_string_0.replace(punctuation[0], "") + punctuation[1];
int index_1 = sentence.indexOf(punctuation[1]);
int index_of_last_space = sentence.lastIndexOf(" ", index_1);
String old_string_1 = sentence.substring(index_of_last_space+1, index_1 + 1);
String replaced_string_1 = punctuation[0] + old_string_1.replace(punctuation[1], "");
sentence = sentence.replace(old_string_0, new_string_0);
sentence = sentence.replace(old_string_1, replaced_string_1);
}
}
现在反转你的字符串。
输入:
“The (quick brown's) fox jumps over the lazy dog”
输出:
“dog lazy the over jumps fox (brown's quick) The”
这可以改进。就像我之前说的,我做的不多 java :/.
我编写了以下代码来反转输入字符串:
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] words = sentence.split(" ");
String reversedSentence = "";
for(int i = words.length - 1; i >= 0 ; i--)
{
reversedSentence += words[i] + " ";
}
System.out.println(reversedSentence);
但是它没有给我想要的结果。我需要标点符号成为它所附单词的一部分,但仍要切换到单词的右侧。例如,如果您输入
“The quick brown fox jumps over the lazy dog”
我希望输出为
“dog lazy the over jumps fox brown quick The”
我实际得到的是:
dog” lazy the over jumps fox brown quick “The
如果只想处理输入首尾的双引号,只需将子串反转,稍后再添加即可。例如
if (sentence.startsWith("\"") && sentence.endsWith("\"")) {
sentence = sentence.substring(1, sentence.length()-1);
}
最后在拆分、反转和连接打印之后:
System.out.println('"' + reversedSentence + '"');
还有 2 条建议:
1) 您的 for 循环留下尾随 space。最后一个字不要加space
2) 你应该使用 StringBuilder
来连接字符串。例如
StringBuilder reversedSentence = new StringBuilder();
for (int i = words.length - 1; i > 0; i--) {
reversedSentence.append(words[i]).append(' ');
}
reversedSentence.append(words[0]);
System.out.println('"' + reversedSentence.toString() + '"');
将字符串分解为数组并使用Collections.reverse()如下:
public class StringReverse {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
String[] array = sentence.replaceAll("\“|”", "").split(" ");
StringBuilder sb = new StringBuilder();
List<String> list = Arrays.asList(array);
Collections.reverse(list);
array = (String[]) list.toArray();
for (int i = 0; i < array.length; i++) {
sb.append(array[i]).append(" ");
}
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
}
用Java8Arrays.stream,就这么简单:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Collectors;
public class StringReverse {
Scanner s = new Scanner(System.in);
System.out.println("Please enter a sentence:");
String sentence = s.nextLine();
StringBuilder sb = new StringBuilder();
Arrays.stream(sentence.replaceAll("\“|”", "").split(" "))
.collect(Collectors.toCollection(ArrayDeque::new))
.descendingIterator()
.forEachRemaining(e -> sb.append(e).append(" "));
System.out.println(sentence.charAt(0)
+ sb.toString().replaceAll(" $", "")
+ sentence.charAt(sentence.length() - 1));
s.close();
}
如果标点符号也打开和关闭。就像你的例子一样。你可以使用这样的东西:
很脏。我稍后会编辑它。我做的不多java。
String[][] punctuations = new String[][] {
{"(", ")"},
{"“", "”"}
};
for (String[] punctuation : punctuations) {
if (sentence.contains(punctuation[0])) {
int index_0 = sentence.indexOf(punctuation[0]);
int index_of_next_space = sentence.indexOf(" ", index_0);
String old_string_0 = sentence.substring(index_0, index_of_next_space);
String new_string_0 = old_string_0.replace(punctuation[0], "") + punctuation[1];
int index_1 = sentence.indexOf(punctuation[1]);
int index_of_last_space = sentence.lastIndexOf(" ", index_1);
String old_string_1 = sentence.substring(index_of_last_space+1, index_1 + 1);
String replaced_string_1 = punctuation[0] + old_string_1.replace(punctuation[1], "");
sentence = sentence.replace(old_string_0, new_string_0);
sentence = sentence.replace(old_string_1, replaced_string_1);
}
}
现在反转你的字符串。
输入:
“The (quick brown's) fox jumps over the lazy dog”
输出:
“dog lazy the over jumps fox (brown's quick) The”
这可以改进。就像我之前说的,我做的不多 java :/.