【问题标题】:Replace words in java using hashmap and properties file使用 hashmap 和属性文件替换 java 中的单词
【发布时间】:2023-03-18 18:24:02
【问题描述】:

如何使用填充属性文件中的值的映射替换单词?

我有这个代码来加载属性文件:

Properties propertiesSlang = new Properties();
FileInputStream fileReadSlang = new FileInputStream(slang);
propertiesSlang.load(fileReadSlang);
System.out.println(propertiesSlang);
Map<String, String> replacements = new HashMap<String, String>((Map)propertiesSlang);

我有很多俚语要替换,我该如何替换俚语?

【问题讨论】:

标签: java replace hashmap tokenize


【解决方案1】:

如果要替换数组中的单词,请使用简单的迭代:

String[] words;
//...
for (int i = 0; i < words.length; i ++) {
    String word = words[i];
    String rep = replacements.get(word);
    if (rep != null) {
        words[i] = rep;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2015-03-02
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多