下面程序段的输出结果为 public class MyClass { public static void main(String args[]) {String s="Helto! How are you";System.out.println(s.lastlndexOf("o",16)); } }
A.16
B.o
C.u
D.17
参考答案:A
解析: 本题考查字符串类中常用成员函数的用法。 String类的成员函数 lastIndexOf()的原型是:public int lastIndexOf(String str,int fromIndex)。它用于获得字符串str在给定字符串中从fromIndex位置往回搜索第一次出现的地方。需要注意的是,在字符串中,下标是从。开始的。所以对于字符串s,下标为16的字母正好是o,从这里往前寻找字符串"o"第一次出现的位置,正好就是字符串中。它本身所在的位置。故s.lastIndexOf ("o",16)返回的结果就是16。