繁体   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