java String类中subSequence和subString方法之间的区别 [英] Difference between subSequence and subString methods in java String class

查看:197
本文介绍了java String类中subSequence和subString方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java String类中subSequence方法和subString方法之间的区别有点混淆。我阅读了文章 String.subString之间有什么区别()和String.subSequence()已经回答了这个问题,但我有点困惑,文章提到它只读,因为你无法改变CharSequence中的字符没有实例化CharSequence的新实例

I have a little confusion regarding the difference between subSequence method and subString method in Java String class. I read the article What is the difference between String.subString() and String.subSequence() which was been answered to this question but I have a little confusion, the article mentioned "Its read only in the sense that you can't change the chars within the CharSequence without instantiating a new instance of a CharSequence".

但当我尝试使用以下示例时

But when I tried with the following example it

String string = "Hello";
CharSequence subSequence = string.subSequence(0, 5);
System.out.println(subSequence.subSequence(1, 4));
subSequence = subSequence.subSequence(1, 4);
System.out.println(subSequence);

打印

ell

ell

it prints
ell
ell

我不知道我是否理解正确。你能帮我澄清一下吗?请告诉我 subString和subSequence 与示例的区别

I do not know whether I have understood it correctly. Can you please help me to clarify this and please tell me the difference between subString and subSequence with an example

非常感谢

推荐答案

正如您链接的文章所述,这两种方法之间的唯一区别是返回类型。一个返回 String ,另一个返回 CharSequence

As explained in the article you linked to, the only difference between those two methods is the return type. One returns a String and the other returns a CharSequence.

要理解的是 CharSequence 是一个接口,而 String 是一个实现 CharSequence

The thing to understand is that CharSequence is an interface, and String is a class which implements CharSequence.

所以每 String 也是 CharSequence ,但反之亦然。您可以将 substring()的输出分配给类型为 CharSequence 的变量,但不是相反:

So every String is also a CharSequence, but not vice versa. You can assign the output of substring() to a variable of type CharSequence, but not the other way around:

String string = "Hello";

String subString1 = string.substring(1,4);             // ell
String subString2 = string.subSequence(1, 4);          // Type mismatch compiler error

CharSequence subSequence1 = string.subSequence(1, 4);  // ell
CharSequence subSequence2 = string.substring(1, 4);    // ell

在这种特殊情况下,因为我们正在执行 subSequence () String 对象上的方法,将调用 String 实现,这只是返回 substring()

In this particular case, since we're executing the subSequence() method on a String object, the String implementation will be invoked, which just returns a substring():

public CharSequence subSequence(int beginIndex, int endIndex) {
    return this.substring(beginIndex, endIndex);
}

这是人们在说 substring()和 subSequence()当你在 String 。

This is what people are talking about when they say the substring() and subSequence() methods are identical when you call them on a String.

关于您发布的代码,因为您开始使用 String ,所有对 subSequence()的调用实际上只是 substring()调用。因此,每当您尝试更改 CharSequence 时,编译器实际上只是创建另一个 String 对象并传递给您引用新的,而不是更改现有对象。

Regarding the code you posted, since you started with a String, all your calls to subSequence() are really just substring() calls under the covers. So each time you try to "change" your CharSequence, the compiler is really just creating another String object and passing you a reference to the new one, rather than changing the existing object.

即使您的变量类型为 CharSequence ,它引用的对象实际上是 String

Even though your variable is of type CharSequence, the object it refers to is really a String.

这篇关于java String类中subSequence和subString方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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