簡體   English   中英

重新組裝拆分字符串基於以前的JAVA中的拆分?

[英]Reassemble split string based on previous split in JAVA?

如果我分割字符串,請這樣說:

List<String> words = Arrays.asList(input.split("\\\\s+"));

然后,我想以各種方式修改這些單詞,然后使用相同的邏輯重新組裝它們,假設單詞的長度沒有變化 ,有沒有辦法輕松地做到這一點? 嘲笑我,這是我這樣做的原因。

注意:我需要匹配所有的whitspace,而不僅僅是空格。 因此正則表達式。

即:

"Beautiful Country" -> ["Beautiful", "Country"] -> ["BEAUTIFUL", "COUNTRY"] -> "BEAUTIFUL COUNTRY"

如果使用String.split ,則無法確保重新組裝的字符串與原始字符串相同。

通常(以及您的情況)無法捕獲所使用的實際分隔符。 在您的示例中, "\\\\s+"將匹配一個或多個空格字符,但您不知道使用了哪些字符,或有多少個字符。

使用split ,有關分隔符的信息將丟失。 期。

(另一方面,如果您不關心重組后的字符串的長度可能不同或與原始字符串的分隔符不同,請使用Joiner類...)

假設您對可以期望的單詞數有限制,則可以嘗試編寫正則表達式,例如

(\S+)(\s+)?(\S+)?(\s+)?(\S+)?

(對於您最多希望輸入三個單詞的情況)。 然后,您可以使用Matcher API方法groupCount(),group(n)拉單個單詞(奇數組)或空格分隔符(偶數組> 0),對單詞進行所需的操作,然后重新組裝它們再來一次...

我嘗試了這個:

import java.util.*;
import java.util.stream.*;
public class StringSplits {
    private static List<String> whitespaceWords = new ArrayList<>();
    public static void main(String [] args) {
        String input = "What a Wonderful World! ...";
        List<String> words = processInput(input);
        // First transformation: ["What", "a", "Wonderful", "World!", "..."]
        String first = words.stream()
                             .collect(Collectors.joining("\", \"", "[\"", "\"]"));
        System.out.println(first);
        // Second transformation: ["WHAT", "A", "WONDERFUL", "WORLD!", "..."]
        String second = words.stream()
                              .map(String::toUpperCase)
                              .collect(Collectors.joining("\", \"", "[\"", "\"]"));
        System.out.println(second);
        // Final transformation: WHAT A WONDERFUL WORLD! ...
        String last = IntStream.range(0, words.size())
                                .mapToObj(i -> words.get(i) + whitespaceWords.get(i))
                                .map(String::toUpperCase)
                                .collect(Collectors.joining());
        System.out.println(last);
    }

    /*
     * Accepts input string of words containing character words and
     * whitespace(s) (as defined in the method Character#isWhitespce).
     * Processes and returns only the character strings. Stores the
     * whitespace 'words' (a single or multiple whitespaces) in a List<String>.
     * NOTE: This method uses String concatenation in a loop. For processing
     * large inputs consider using a StringBuilder.
     */
    private static List<String> processInput(String input) {
        List<String> words = new ArrayList<>();
        String word = "";
        String whitespaceWord = "";
        boolean wordFlag = true;
        for (char c : input.toCharArray()) {
            if (! Character.isWhitespace(c)) {
                if (! wordFlag) {
                    wordFlag = true;
                    whitespaceWords.add(whitespaceWord);
                    word = whitespaceWord = "";
                }
                word = word + String.valueOf(c);
            }   
            else {
                if (wordFlag) {
                    wordFlag = false;
                    words.add(word);
                    word = whitespaceWord = "";
                }
                whitespaceWord = whitespaceWord + String.valueOf(c);
            }
        } // end-for
        whitespaceWords.add(whitespaceWord);    
        if (! word.isEmpty()) {
            words.add(word);
        }
        return words;
    }
}

暫無
暫無

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

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