Java subString and indexOf



我得到了以下问题。

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真的让我感到困惑。

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

谢谢。

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

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

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

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

最后,string3 的第三个值是之前的任何字符串 3 加上大写字母的 string2。这是

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

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

最新更新