简体   繁体   English

如何从字符串中删除符号并将它们放回去?

[英]How to remove symbols from string and put them back?

Global task is to swap longest word with shortest word in text.全局任务是将文本中最长的单词与最短的单词交换。 I need to remove symbols for example ,.??我需要删除符号,例如,.?? from text(to count length of word) and update array of words, do some logic and after put them back.从文本(计算单词的长度)和更新单词数组,做一些逻辑,然后把它们放回去。

My solution:我的解决方案:


Map<Integer, String> idxToSymbolMap = new HashMap<>();

// remove symbols and save them to map
void deleteSymbols(String[] array) {
        String[] symbols = {POINT, QUESTION_MARK, EXCLAMATION_MARK, COMMA};
        for (int i = 0; i < array.length; i++) {
            for (String s : symbols) {
                if (array[i].contains(s)) {
                    array[i] = array[i].replace(s, EMPTY_STRING);
                    idxToSymbolMap.put(i, s);
                }
            }
        }
    }
// some code here...

// add symbols back
void addBackSymbols(String[] array) {
        for (int i = 0; i < array.length; i++) {
            int idx = i;
            idxToSymbolMap.forEach((k, v) -> {
                if (k == idx) {
                    array[idx] +=  v;
                }
            });
        }
    }

How could I do that better, cleaner?我怎样才能做得更好,更清洁? Is possible using replaceAll or something else?是否可以使用 replaceAll 或其他东西?

I guess what you have is the right approach, imo.我想你所拥有的是正确的方法,imo。

However, you can make it cleaner:但是,您可以使其更清洁:

String[] symbols = {".", "?", "!", ","};
for (int i = 0; i < array.length; i++) {
    for (String s: symbols) {
        if (array[i].contains(s)) {
            array[i] = array[i].replace(s, "");
            idxToSymbolMap.put(i, s);
        }
    }
}

Or using java 8+:或使用 java 8+:

IntStream.range(0, array.length).forEach(i ->
        Arrays.stream(symbols).filter(s -> array[i].contains(s)).forEach(s -> {
            idxToSymbolMap.put(i, s);
            array[i] = array[i].replace(s, "");
        }));

And replace it:并替换它:

IntStream.range(0, array.length).forEach(
    idx -> idxToSymbolMap.keySet().stream().filter(key -> key == idx)
        .forEach(key -> array[idx] += idxToSymbolMap.get(key)));

Map<String, List<Integer>> will be the best fit for this requirement where the key is the symbol and value is the list of indices where the symbol exists in the text. Map<String, List<Integer>>将最适合此要求,其中键是符号,值是符号存在于文本中的索引列表。

For the sake of testing, let's replace each occurrence of [.,??] with # , print the replaced string and get back the original string.为了测试,让我们用#替换每个出现的[.,??] ,打印替换的字符串并取回原始字符串。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Map<Character, List<Integer>> symbolToIdxMap = new HashMap<>();

        // Test string
        String text = "abc .123,  xyz! hello . hi";

        // Regex pattern
        String strPattern = "[\\.\\,\\!\\?]";
        Pattern pattern = Pattern.compile(strPattern);
        Matcher matcher = pattern.matcher(text);
        int index;

        // Store the symbols and their indices in the map
        while (matcher.find()) {
            // Index at which any of [.,!?] is found
            index = matcher.start();

            // Character at `index`
            Character symbol = text.charAt(index);

            // If the List<Integer> for `symbol` exists, get it return a new
            // ArrayList<Integer> to `idxList`
            List<Integer> idxList = symbolToIdxMap.getOrDefault(symbol, new ArrayList<Integer>());

            // Add `index` to `idxList`
            idxList.add(index);

            // Put `idxList` as the value of `symbol`
            symbolToIdxMap.put(symbol, idxList);
        }

        // Replace the symbols in the text
        String replaced = text.replaceAll(strPattern, "#");
        System.out.println("Replaced: " + replaced);

        // Restore original string
        StringBuilder sb = new StringBuilder(replaced);
        for (Entry<Character, List<Integer>> entry : symbolToIdxMap.entrySet()) {
            // Replace the character at all indices in the list with the corresponding
            // character
            for (Integer idx : entry.getValue()) {
                sb.setCharAt(idx, entry.getKey());
            }
        }

        // Display original string
        System.out.println("Original: " + sb);
    }
}

Output: Output:

Replaced: abc #123#  xyz# hello # hi
Original: abc .123,  xyz! hello . hi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM