0

我收到了以下问题。

String string1 = "The ice cold ice-cream";
String string2 = "phone";
String string3 = "";
int p = string1.indexOf("ice",8);
string3 = string1.substring(0, p + 1);
string3 = string3 + string2.toUpperCase();

我似乎无法获得第二个和第三个值。

子字符串和int p真的让我很困惑。

任何人都可以帮助回答并解释如何解决这个问题。

谢谢。

4

1 回答 1

6

string3 的第一个值是一个空字符串。

现在 p 是 string1 中“ice”的索引,出现在第 8 个索引之后。所以p是13(你可以自己数)。

string3 的第二个值是 string1 从第 0 个索引到第 (13+1) 个索引。或 string1 从 0 到第 14 个索引。结果是

"The ice cold i" // note the 14th index is not included

最后,string3 的第三个值是 string3 之前的值加上 string2 的大写字母。哪个是

"The ice cold i" + "PHONE" or simply "The ice cold iPHONE"

如果您在使用 String 方法时遇到问题,您应该查看http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

于 2013-11-14T02:54:30.560 回答