黑马程序员技术交流社区

标题: replace 用法的问题,求解答! [打印本页]

作者: zhang214214    时间: 2015-4-29 20:18
标题: replace 用法的问题,求解答!
       public class Test6 {
                public static void main(String[] args) {
                        String s = "abc";
                        s.substring(1);
                        s.replace("bc", "xyz");    //这句提示错误;错误信息如下,求解!!!
                       System.out.println(s);
                       String value = new String ("abc");
                       System.out.println(s == value);
                }
       }



Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
        The method replace(char, char) in the type String is not applicable for the arguments (String, String)

        at com.itheima.Test6.main(Test6.java:25)  //25行就是错误行.
作者: shield    时间: 2015-4-30 08:53
额。。 看不到二十五行这么远。。。
还有就是substring 跟replace 都是返回一个String类型的值。 你这样单独写在一行没什么意义
作者: 考不上黑马    时间: 2015-4-30 10:48
发帖子的时候里面有个插入代码,你把全部代码贴出来吧。你的这部分代码运行是正常的
作者: Chans_宇    时间: 2015-4-30 10:53
我拿去验证过了,这段代码没报错……你骗我……:'(
作者: Chans_宇    时间: 2015-4-30 10:58
s.substring(1); s.replace("bc", "xyz"); 你这两句都只是利用String的截取和替换功能生成了新的字符串而已,并无赋值引用,所以s的值还是“abc”,在内存运行的话栈内存变量s还是指向“abc”这里的物理地址的:D
作者: zhang214214    时间: 2015-4-30 16:52
难道是我的  eclipse 的问题吗?  它真的就是出错,运行不了= =;我也觉得奇怪,但是  eclipse  确实报错.......
作者: 智远    时间: 2015-4-30 19:37
zhang214214 发表于 2015-4-30 16:52
难道是我的  eclipse 的问题吗?  它真的就是出错,运行不了= =;我也觉得奇怪,但是  eclipse  确实报错...... ...

真没错。。。。,重启一下
作者: 挥斥方遒    时间: 2015-4-30 20:16
明明写着 replace(char, char) 你干嘛非要写字符串啊?
作者: rightyuan521    时间: 2015-4-30 22:15
我运行一下,没报错啊
作者: rightyuan521    时间: 2015-4-30 22:16
结果为abc false




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2