【发布时间】:2021-06-30 02:55:49
【问题描述】:
我是字符串数组的新手,并且有一个关于用句子中的特定单词替换四个字母单词的问题。例如,我得到一个字符串作为输入,如
Being good to them, always helps!
将四个字母的单词替换为单词love,例如
Being love to love, always helps!
如何将一个句子的四个字母单词全部拆分,然后放到一个字符串数组中,然后做剩下的事情?
【问题讨论】:
我是字符串数组的新手,并且有一个关于用句子中的特定单词替换四个字母单词的问题。例如,我得到一个字符串作为输入,如
Being good to them, always helps!
将四个字母的单词替换为单词love,例如
Being love to love, always helps!
如何将一个句子的四个字母单词全部拆分,然后放到一个字符串数组中,然后做剩下的事情?
【问题讨论】:
使用String.replaceAll 和以下正则表达式来查找4 个字母的单词:"\b\w{4}\b":
String sentence = "Being good to them, always helps!";
String fixed = sentence.replaceAll("\\b\\w{4}\\b", "love");
System.out.println(fixed);
输出
Being love to love, always helps!
这允许在不创建大量中间字符串及其数组的情况下获取结果字符串。
如果真的需要在拆分后有这样的数组,替换这个数组中的单词,然后重建结果字符串,可以如下进行。
另外,在下面的代码中,另一个字符类"\p{L}{4}" 用于匹配由只有字母 组成的4 字母单词,包括非英文字母。 (上面使用的类\w是数字、下划线和英文字母的快捷方式,[A-Za-z0-9_])
String sentence = sentence = "1234 is not a word. Neither A_Z1. O'Neil in New-York. Орут коты.";
String[] parts = sentence.split("\\b");
for (int i = 0; i < parts.length; i++) {
if (parts[i].matches("\\p{L}{4}")) {
parts[i] = "love";
}
}
System.out.println(String.join("", parts));
输出(与 4 位数字相反,不替换 4 字符空格):
1234 is not a love. Neither A_Z1. O'love in New-love. love love.
另外,当Stream<String>可以从Pattern::splitAsStream获取,结果通过Collectors.joining获取时,可以使用Stream API实现结果的替换和检索:
String str = Pattern.compile("\\b").splitAsStream(sentence) // Stream<String>
.map(s -> s.matches("\\w{4}") ? "love" : s)
.collect(Collectors.joining(""));
System.out.println(str);
输出(与非英文单词不同,用数字替换“单词”)
love is not a love. Neither love. O'love in New-love. Орут коты.
【讨论】:
你可以使用String#replaceAll方法:
String str1 = "Being good to them, always helps!";
// regex groups:
// (^|\\W) - $1 - beginning of a string or a non-word character
// (\w{4}) - $2 - sequence of 4 word characters
// (\W|$) - $3 - non-word character or end of a string
String str2 = str1.replaceAll("(^|\\W)(\\w{4})(\\W|$)", "$1love$3");
System.out.println(str2);
输出:
Being love to love, always helps!
【讨论】:
这是一个可以满足您要求的程序:
String s = "I hate you, you doer"
var array = s.split("\\b");
var list = new ArrayList<>(Arrays.asList(array));
while (list.remove(" ")) {};
List<String> result=new ArrayList<String>();
for (String word:list)
if (word.length()==4)
result.add("love");
else
result.add(word);
System.out.println(String.join(" ", result).replace(" , ", ",")
输出是:
I love you, you love
这就是我的想法。 我使用了 Java REPL,因为它使探索更容易。 我忽略了逗号,假装它是一个单词的字母。如果您需要处理标点符号,事情会变得更糟。
$ jshell
| Welcome to JShell -- Version 11.0.10
| For an introduction type: /help intro
jshell> String s = "I hate you, you doer"
s ==> "I hate you, you doer"
jshell> var array = s.split("\\b");
array ==> String[9] { "I", " ", "hate", " ", "you", ", ", "you", " ", "doer" }
jshell> for (String word:array) if (word.length()==4) System.out.println(word);
...>
hate
doer
jshell> var list = new ArrayList<>(Arrays.asList(array));
list ==> [I, , hate, , you, , , you, , doer]
while (list.remove(" ")) {};
jshell> list
list ==> [I, hate, you, , , you, doer]
jshell> List<String> result=new ArrayList<String>();
result ==> []
jshell> for (String word:list) if (word.length()==4) result.add("love"); else result.add(word);
...>
jshell> result
result ==> [I, love, you, , , you, love]
jshell> String.join(" ", result).replace(" , ", ",")
$17 ==> "I love you, you love"
【讨论】: