Java subString和indexOf

Seb*_*lch 0 java string substring

我收到了以下问题.

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();
Run Code Online (Sandbox Code Playgroud)

我似乎无法获得第二和第三的价值.

子串,int p真让我困惑.

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

谢谢.

sli*_*der 6

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

现在p是第8个索引之后发生的string1中的"ice"索引.所以p是13(你可以自己算一下).

string3的第二个值是从第0个索引到第(13 + 1)个索引的string1.或者string1从0到第14个索引.那就是

"The ice cold i" // note the 14th index is not included
Run Code Online (Sandbox Code Playgroud)

最后,string3的第三个值是前面的string3和大写字母中的string2.这是

"The ice cold i" + "PHONE" or simply "The ice cold iPHONE"
Run Code Online (Sandbox Code Playgroud)

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