0
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 は、変数値に含まれるものを正確に提供し、上記の単語を削除しません。

これは、weatherLocation を文字列の配列、たとえば weatherLoc の配列に分割したときに機能し、それらのコード行は weatherLoc[1] で機能しました

私は何を間違っていますか?

4

4 に答える 4

1

メソッド呼び出しによって返された値を String 参照変数に代入する必要があります。を行うたびに、新しいオブジェクトがreplaceAll()返されますが、変数はまだ元の文字列を参照しています。 StringweatherLocation

  weatherLocation = weatherLocation.replaceAll("how","")
                    .replaceAll("what","")
                    .replaceAll("weather", "")
                    .replaceAll("like", "")
                    .replaceAll(" in", "")
                    .replaceAll(" at", "")
                    .replaceAll("around", "");
于 2013-07-09T16:14:13.783 に答える
0

StringですimmutableString.replaceAllの新しいinstanceを返しますString。したがって、次のように使用する必要があります

weatherLocation = weatherLocation.replaceAll("how","")
                .replaceAll("what","")
                .replaceAll("weather", "")
                .replaceAll("like", "")
                .replaceAll(" in", "")
                .replaceAll(" at", "")
                .replaceAll("around", "");
于 2013-07-09T16:14:36.743 に答える
0

文字列は不変です。これらすべての replaceAll 呼び出しの値を変数に割り当てる必要があり、それが必要になります。

weatherLocation = weatherLocation.replaceAll("how","")
                .replaceAll("what","")
                .replaceAll("weather", "")
                .replaceAll("like", "")
                .replaceAll(" in", "")
                .replaceAll(" at", "")
                .replaceAll("around", "");
于 2013-07-09T16:14:11.853 に答える
0

これを試して:

weatherLocation = weatherLocation.replaceAll("how","")
                        .replaceAll("what","")
                        .replaceAll("weather", "")
                        .replaceAll("like", "")
                        .replaceAll(" in", "")
                        .replaceAll(" at", "")
                        .replaceAll("around", "");
于 2013-07-09T16:14:26.723 に答える