Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-一次更改字符串中的多个单词?_Java_String - Fatal编程技术网

Java-一次更改字符串中的多个单词?

Java-一次更改字符串中的多个单词?,java,string,Java,String,我正在尝试创建一个程序,可以在用户给定的字符串中缩写某些单词 到目前为止,我是这样安排的: 从.txt文件创建哈希映射,如下所示: thanks,thx your,yr probably,prob people,ppl 从用户处获取字符串 将字符串拆分为单词 检查hashmap以查看该单词是否作为键存在 使用hashmap.get()返回键值 用返回的键值替换单词 返回更新的字符串 在我尝试更新字符串之前,一切都非常正常: public String shortenMessage( Str

我正在尝试创建一个程序,可以在用户给定的字符串中缩写某些单词

到目前为止,我是这样安排的:

从.txt文件创建哈希映射,如下所示:

thanks,thx
your,yr
probably,prob
people,ppl
  • 从用户处获取字符串
  • 将字符串拆分为单词
  • 检查hashmap以查看该单词是否作为键存在
  • 使用hashmap.get()返回键值
  • 用返回的键值替换单词
  • 返回更新的字符串
在我尝试更新字符串之前,一切都非常正常:

public String shortenMessage( String inMessage ) {

    String updatedstring = "";
    String rawstring = inMessage;
    String[] words = rawstring.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");

    for (String word : words)  {  
        System.out.println(word);
        if (map.containsKey(word) == true) {
            String x = map.get(word);
        updatedstring = rawstring.replace(word, x);
        }
    }  

    System.out.println(updatedstring); 
    return updatedstring;
}
输入:

thanks, your, probably, people
输出:

thanks, your, probably, ppl
有人知道如何更新字符串中的所有单词吗

提前谢谢

updatedstring = rawstring.replace(word, x);
这会不断地用单个替换的原始字符串替换updatedstring

你需要像这样做

updatedstring = rawstring;
...

updatedString = updatedString.replace(word, x);

编辑:

这就是您看到的问题的解决方案,但您的代码还有一些其他问题:

  • 对于需要小写或从中删除字符的内容,替换将不起作用。您可以创建单词数组,从修改后的rawstring版本中迭代该数组。然后返回并尝试替换原始原始原始字符串中不存在的修改版本。这将找不到您认为要替换的单词

  • 如果您正在进行全局替换,您可以只创建一组单词而不是数组,因为一旦替换了单词,它就不会再次出现

  • 您可能希望一次替换一个单词,因为您的全局替换可能会导致奇怪的错误,其中替换映射中的一个单词是另一个替换单词的子单词。不要使用String.replace,而是创建一个单词数组/列表,迭代单词,根据需要替换列表中的元素并将它们连接起来。在java 8中:

    String.join(“,元素)

  • 这会不断地用单个替换的原始字符串替换updatedstring

    你需要像这样做

    updatedstring = rawstring;
    ...
    
    updatedString = updatedString.replace(word, x);
    

    编辑:

    这就是您看到的问题的解决方案,但您的代码还有一些其他问题:

  • 对于需要小写或从中删除字符的内容,替换将不起作用。您可以创建单词数组,从修改后的rawstring版本中迭代该数组。然后返回并尝试替换原始原始原始字符串中不存在的修改版本。这将找不到您认为要替换的单词

  • 如果您正在进行全局替换,您可以只创建一组单词而不是数组,因为一旦替换了单词,它就不会再次出现

  • 您可能希望一次替换一个单词,因为您的全局替换可能会导致奇怪的错误,其中替换映射中的一个单词是另一个替换单词的子单词。不要使用String.replace,而是创建一个单词数组/列表,迭代单词,根据需要替换列表中的元素并将它们连接起来。在java 8中:

    String.join(“,元素)


  • Debug
    再多一点。在您的
    if(map.containsKey(word))
    中打印
    x
    ,然后打印
    updatedstring
    ,并查看结果。编辑:我有自己的想法(虽然我不确定),但@DTing指出并证实了我的想法。永远不要这样做-
    if(map.containsKey(word)==true)
    ;始终编写
    if(map.containsKey(word))
    。马上改掉这个习惯。
    Debug
    再多一点。在您的
    if(map.containsKey(word))
    中打印
    x
    ,然后打印
    updatedstring
    ,并查看结果。编辑:我有自己的想法(虽然我不确定),但@DTing指出并证实了我的想法。永远不要这样做-
    if(map.containsKey(word)==true)
    ;始终编写
    if(map.containsKey(word))
    。马上改掉那个习惯。