Java 用另一个多字符串替换该多字符串

Java 用另一个多字符串替换该多字符串,java,string,Java,String,我有像- 是的,我有两个,三个,四个号码 我有五、六、七个字母表 八,二,五是我的幸运数字 我还有地图 Map <String, String> wordToDigit = new HashMap<>; Map.put("two", 2); Map.put("three", 3); Map.put("four", 4); Map.put("five", 5); Map.put("six", 6); Map.put("eight",8); 我会把句子分成几个单词,然后重建

我有像-

  • 是的,我有两个,三个,四个号码
  • 我有五、六、七个字母表
  • 八,二,五是我的幸运数字
  • 我还有地图

    Map <String, String> wordToDigit = new HashMap<>;
    Map.put("two", 2);
    Map.put("three", 3);
    Map.put("four", 4);
    Map.put("five", 5);
    Map.put("six", 6);
    Map.put("eight",8);
    

    我会把句子分成几个单词,然后重建句子,用你的地图检查每个单词是否有数字版本,如果有,用数字来代替单词

    诀窍是重新添加分隔符,因为标准的
    String.split
    方法不包括这些分隔符

    Map <String, Integer> wordToDigit = new HashMap<>();
    wordToDigit.put("two", 2);
    wordToDigit.put("three", 3);
    wordToDigit.put("four", 4);
    wordToDigit.put("five", 5);
    wordToDigit.put("six", 6);
    wordToDigit.put("eight",8); 
    
    String sentence = "yes, I have two, three, four numbers";
    
    String[] words = sentence.split("[^a-zA-Z]+", -1);
    
    int pos = 0;
    StringBuilder sb = new StringBuilder();
    for(String word : words)
    {
        int idx = sentence.indexOf(word, pos);
        String delim = sentence.substring(pos, idx);
        sb.append(delim);        
    
        Integer n = wordToDigit.get(word.toLowerCase());
        sb.append(n == null ? word : n);
    
        pos += delim.length() + word.length();      
    }
    sb.append(sentence.substring(pos, sentence.length()));        
    
    System.out.println(sb);
    
    另外两句话呢

    I have 5, 6, seven alphabets
    8, 2, 5 are my lucky numbers.
    

    我会把句子分成几个单词,然后重建句子,用你的地图检查每个单词是否有数字版本,如果有,用数字来代替单词

    诀窍是重新添加分隔符,因为标准的
    String.split
    方法不包括这些分隔符

    Map <String, Integer> wordToDigit = new HashMap<>();
    wordToDigit.put("two", 2);
    wordToDigit.put("three", 3);
    wordToDigit.put("four", 4);
    wordToDigit.put("five", 5);
    wordToDigit.put("six", 6);
    wordToDigit.put("eight",8); 
    
    String sentence = "yes, I have two, three, four numbers";
    
    String[] words = sentence.split("[^a-zA-Z]+", -1);
    
    int pos = 0;
    StringBuilder sb = new StringBuilder();
    for(String word : words)
    {
        int idx = sentence.indexOf(word, pos);
        String delim = sentence.substring(pos, idx);
        sb.append(delim);        
    
        Integer n = wordToDigit.get(word.toLowerCase());
        sb.append(n == null ? word : n);
    
        pos += delim.length() + word.length();      
    }
    sb.append(sentence.substring(pos, sentence.length()));        
    
    System.out.println(sb);
    
    另外两句话呢

    I have 5, 6, seven alphabets
    8, 2, 5 are my lucky numbers.
    

    下面给出了两种不同的实现:(1)使用
    Map
    (2)使用
    List
    和数组:

    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            // Test
            String[] sentences = { "yes, I have two, three, four numbers.", "I have five, six, seven alphabets.",
                    "eight, two, five are my lucky numbers.", "Someone gave him FIVE canines and asked him to feed two." };
            System.out.println("Using Map: ");
            for (String sentence : sentences) {
                System.out.println(replaceWordsWithValue1(sentence));
            }
            System.out.println("Using List and array: ");
            for (String sentence : sentences) {
                System.out.println(replaceWordsWithValue2(sentence));
            }
        }
    
        static String replaceWordsWithValue1(String str) {
            Map<String, String> wordToDigit = Map.of("two", "2", "three", "3", "four", "4", "five", "5", "six", "6",
                    "seven", "7", "eight", "8", "nine", "9");
            for (String key : wordToDigit.keySet()) {
                Pattern pattern = Pattern.compile("\\b" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(str);
                while (matcher.find()) {
                    str = str.replaceAll(matcher.group(), wordToDigit.get(key));
                }
            }
            return str;
        }
    
        static String replaceWordsWithValue2(String str) {
            String[] values = { "2", "3", "4", "5", "6", "7", "8", "9" };
            List<String> keys = List.of("two", "three", "four", "five", "six", "seven", "eight", "nine");
            for (String key : keys) {
                Pattern pattern = Pattern.compile("\\b" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(str);
                while (matcher.find()) {
                    str = str.replaceAll(matcher.group(), values[keys.indexOf(key)]);
                }
            }
            return str;
        }
    }
    

    两种实现中的逻辑都是直截了当的。但是,如果有任何疑问/问题,请随时发表评论。

    下面给出了两种不同的实现方式:(1)使用
    映射(2)使用
    列表和数组:

    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Main {
    
        public static void main(String[] args) throws IOException {
            // Test
            String[] sentences = { "yes, I have two, three, four numbers.", "I have five, six, seven alphabets.",
                    "eight, two, five are my lucky numbers.", "Someone gave him FIVE canines and asked him to feed two." };
            System.out.println("Using Map: ");
            for (String sentence : sentences) {
                System.out.println(replaceWordsWithValue1(sentence));
            }
            System.out.println("Using List and array: ");
            for (String sentence : sentences) {
                System.out.println(replaceWordsWithValue2(sentence));
            }
        }
    
        static String replaceWordsWithValue1(String str) {
            Map<String, String> wordToDigit = Map.of("two", "2", "three", "3", "four", "4", "five", "5", "six", "6",
                    "seven", "7", "eight", "8", "nine", "9");
            for (String key : wordToDigit.keySet()) {
                Pattern pattern = Pattern.compile("\\b" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(str);
                while (matcher.find()) {
                    str = str.replaceAll(matcher.group(), wordToDigit.get(key));
                }
            }
            return str;
        }
    
        static String replaceWordsWithValue2(String str) {
            String[] values = { "2", "3", "4", "5", "6", "7", "8", "9" };
            List<String> keys = List.of("two", "three", "four", "five", "six", "seven", "eight", "nine");
            for (String key : keys) {
                Pattern pattern = Pattern.compile("\\b" + Pattern.quote(key) + "\\b", Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(str);
                while (matcher.find()) {
                    str = str.replaceAll(matcher.group(), values[keys.indexOf(key)]);
                }
            }
            return str;
        }
    }
    

    两种实现中的逻辑都是直截了当的。但是,如果有任何疑问/问题,请随时发表评论。

    ?请显示您期望的输出。嗯?请显示您期望的输出。