Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 删除所有数字和“;{quot;来自字符串数组_Java_Arrays_Replaceall - Fatal编程技术网

Java 删除所有数字和“;{quot;来自字符串数组

Java 删除所有数字和“;{quot;来自字符串数组,java,arrays,replaceall,Java,Arrays,Replaceall,我正在尝试根据需要从字符串数组中删除所有非数字字符,以便将其与数字列表进行比较。拆分工作正常,但我无法比较这两组字符 for(String w:a1) { w=w.replaceAll("[^\\d.]", ""); if(dContacts.getNumber().equals(w)) { System.out.println("Compared1234567"); } Syst

我正在尝试根据需要从字符串数组中删除所有非数字字符,以便将其与数字列表进行比较。拆分工作正常,但我无法比较这两组字符

for(String w:a1)
    {
        w=w.replaceAll("[^\\d.]", "");

        if(dContacts.getNumber().equals(w))
        {
            System.out.println("Compared1234567");
        }

        System.out.println("---6545678909876789876hijkhijkhijkjh"+dContacts.getNumber());
        System.out.println("Arraylistextract"+w);
    }

在Java中,字符串是不可变的。这意味着
w.replaceAll(“[^\\d.]”,”);
将创建不同的字符串,而
w
将保持不变。将该表达式指定给
w
以存储它:

w = w.replaceAll("[^\\d.]", "");

如果不将字符串重新指定给同一变量或新变量,则无法修改该字符串。

字符串是不可变的,因此如果调用某个函数,则该字符串不会在同一对象中更改

你必须把这行改成

w= w.replaceAll("[^\\d.]", "");
为什么
String a=“hello”;a.concat(“world!”);System.out.println(a);
不打印“hello world!”?