簡體   English   中英

用特定單詞替換字符串的四個字母單詞

[英]Replacing four letter words of a string with a specific word

我是字符串 arrays 的新手,並且有一個關於用句子中的特定單詞替換四個字母單詞的問題。 例如,我得到一個字符串作為輸入

Being good to them, always helps!

love這個詞替換四個字母的詞,例如,這將是

Being love to love, always helps!

如何拆分句子的所有四個字母單詞,然后將其放入字符串數組並執行 rest?

使用帶有以下正則表達式的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);

Output

Being love to love, always helps!

這允許在不創建大量中間字符串及其數組的情況下獲取結果字符串。


如果確實需要拆分后有這樣的數組,則替換該數組中的單詞,然后重建結果字符串,可以如下進行。

此外,在下面的代碼中,另一個字符 class "\p{L}{4}"用於匹配僅由字母組成的 4 字母單詞,包括非英文字母。 (上面使用的 class \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));

Output(與 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);

Output(“單詞”用數字替換,與非英文單詞不同)

love is not a love.    Neither love. O'love in New-love. Орут коты.

這是一個可以滿足您要求的程序:

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(" , ", ",")

Output 是:

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"

您可以使用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);

Output:

Being love to love, always helps!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM