使用replace和substring方法进行字符串相等 [英] String equality using replace and substring methods

查看:234
本文介绍了使用replace和substring方法进行字符串相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对字符串比较有一些疑问,尤其是关于替换和子字符串方法,我找不到任何答案,所以这是代码

I have some questions about string comparison.I couln't find any answers concerning replace and substring methods in particular.So here is the code

public static void main(String args[]) {
    if ("hello".substring(0) == "hello")
        System.out.println("statement 1 is true");
    if ("hello".substring(1) == "ello")
        System.out.println("statement 2 is true");
    if ("hello".replace('l', 'l') == "hello")
        System.out.println("statement 3 is true");
    if ("hello".replace('h', 'H') == "Hello")
        System.out.println("statement 4 is true");
    if ("hello".replace('h', 'H') == "hello".replace('h', 'H'))
        System.out.println("statement 5 is true");
}

输出为:

statement 1 is true
statement 3 is true

substring方法是否创建了一个新的String()。如果是,为什么一个语句为true,而第二个语句却不是?相同的问题是关于语句3和4.的问题。

Does the substring method create a new String().If so why is statement one true,yet 2 is not?Same question goes about statement 3 and 4.Thank you.

推荐答案

我假设您了解字符串比较的工作原理。因此,我将尝试解释发生了什么。

I assume you are aware of how string comparison works. So i'll try to explain what is happening.

java中的字符串是不可变的对象,因此一旦创建,就无法更改它们。
为了减少一遍又一遍地创建相同字符串对象的开销,有一个已经使用过/创建的字符串池。
现在,当您比较两个字符串对象是否相同时,它会比较对象本身是否相同。如果执行 hello .substr(0),它将创建一个带有 hello的字符串对象。由于已经使用了 hello,因此包含 hello的字符串对象已经在字符串对象池中。而且,如果您现在再次创建字符串对象 hello进行比较,它将从池中返回相同的对象。

Strings in java are immutable objects, so once created you can't change them. To reduce overhead in creating the "same" string object over and over again, there is a pool of already used/created strings. Now when you now compare if two string objects are the same it compares whether the objects themselves are the same. if you do "hello".substr(0) it will "create" a string object with "hello" in it. Since "hello" was already used, the string object containing "hello" is already in the string object pool. And if you now again "create" the string object "hello" to compare with, it will return the same object from the pool.

hello .replace( l, l)它将返回与上述相同的 hello字符串对象。
但是您不能依靠它,因为字符串池可以随时清空。

The same is happening with the "hello".replace("l","l") it will return the same "hello" string object like above. But you can not rely on that, because the string-pool can get emptied at any time.

进一步阅读内部字符串 http://docs.oracle.com/javase/6 /docs/api/java/lang/String.html#intern()

Further reading for "internalling strings" http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#intern()

但是,如果您的问题是关于如何比较字符串本身而不是对象包含您的字符串,您应该真正使用 hello .equals( hello),因为这样它将比较实际内容。 (如上所述)

BUT, if your question is about how to compare string themselves not the objects containing you strings, you should really use "hello".equals("hello") because then it will compare the real content. (As mentioned above)

这篇关于使用replace和substring方法进行字符串相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆