简体   繁体   English

无法使用replaceAll从字符串中删除单词

[英]Can't delete words from a string with replaceAll

if(containsAllWeather || containsAllWeather2){

            String weatherLocation = value.toString();

        if (weatherLocation != null){


                weatherLocation.replaceAll("how","")
                        .replaceAll("what","")
                        .replaceAll("weather", "")
                        .replaceAll("like", "")
                        .replaceAll(" in", "")
                        .replaceAll(" at", "")
                        .replaceAll("around", "");
        }

weatherLocation still gives exactly what the variable value includes and doesn't delete any of the words listed above. weatherLocation仍会准确给出变量包含的内容,并且不会删除上面列出的任何单词。

This worked when I split weatherLocation to an array of strings, say, array of weatherLoc, and those lines of code worked for weatherLoc[1] 当我将weatherLocation拆分为一个字符串数组(例如,weatherLoc数组)并且这些代码行用于weatherLoc [1]时,此方法有效

what am I doing wrong? 我究竟做错了什么?

You need to assign the values returned by the method calls back to the String reference variable. 您需要将方法调用返回的值分配回String引用变量。 Each time you do replaceAll() , it returns a new String object but your weatherLocation variable is still referencing to the original String. 每次您执行replaceAll() ,它都会返回一个新的 String对象,但是您的weatherLocation变量仍引用原始String。

  weatherLocation = weatherLocation.replaceAll("how","")
                    .replaceAll("what","")
                    .replaceAll("weather", "")
                    .replaceAll("like", "")
                    .replaceAll(" in", "")
                    .replaceAll(" at", "")
                    .replaceAll("around", "");

Strings are immutable. 字符串是不可变的。 You need to assign the value of all those replaceAll calls to a variable and that will be what you want. 您需要将所有那些replaceAll调用的值分配给一个变量,这就是您想要的。

weatherLocation = weatherLocation.replaceAll("how","")
                .replaceAll("what","")
                .replaceAll("weather", "")
                .replaceAll("like", "")
                .replaceAll(" in", "")
                .replaceAll(" at", "")
                .replaceAll("around", "");

Try this: 尝试这个:

weatherLocation = weatherLocation.replaceAll("how","")
                        .replaceAll("what","")
                        .replaceAll("weather", "")
                        .replaceAll("like", "")
                        .replaceAll(" in", "")
                        .replaceAll(" at", "")
                        .replaceAll("around", "");

String is immutable . Stringimmutable So String.replaceAll returns a new instance of String . 因此, String.replaceAll返回String的新instance So you need to use like following 所以你需要像下面这样使用

weatherLocation = weatherLocation.replaceAll("how","")
                .replaceAll("what","")
                .replaceAll("weather", "")
                .replaceAll("like", "")
                .replaceAll(" in", "")
                .replaceAll(" at", "")
                .replaceAll("around", "");

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

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