首页 > 试题广场 >

执行下列程序的输出结果为( ) public class o

[单选题]
执行下列程序的输出结果为(      )
public class onetest {
    public static void main(String args[]) {
        String s = "xxxxxxxxxxxxxxx#123#456#zzzzz";  
        int n = s.indexOf("#");      
        int k = s.indexOf("#", n+1);  
        String s2 = s.substring(n+2, k);  
        System.out.println(s2); 
    }
}
  • 12
  • 23
  • 123
  • #123
n是第一个#的索引,indexOf传第二个参数说明从n+1开始找#,因此找到的是第二个#,最后substring操作不会截取到第2个#,结果为23
发表于 2021-09-03 17:21:53 回复(0)
String.indexOf(int ch):返回此字符串中第一次出现指定字符的索引,没有返回-1
String.indexOf(int ch,int fromIndex):返回此字符串中第一次出现指定字符的索引,从指定索引开始搜索,没有返回-1
subString左闭右开


编辑于 2023-04-05 11:21:00 回复(0)