怎么从字符串中截取到自己想要的字符?

    public static void main(String[] args) {
        //这是一个字符串 我要截取它的a n
        String s = "renguanyu";
        int x = s.indexOf("a");
        System.out.println("开始索引" + x);
        int y = s.indexOf("n");
        System.out.println("结束索引" + y);
        String newstr = s.substring(x, y);
        System.out.println(newstr);
    }
    public static void main(String[] args) {
        // 这是一个字符串 我要截取它的a n
        String s1 = "renguanyu";
        String s2 = "an";
        int anindex = s1.indexOf(s2);
        String s3 = s1.substring(anindex,anindex+s2.length());
        System.out.println(s3);
    }

它获取的是第一个n的位置,所以不能获取。

不知道什么意思。。。。。想要字符还是要一个字符串?

跟楼上一样,你是想要a到n之间的字符串,还是就像把a和n提出来啊