开发者社区 问答 正文

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

 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);
    }

展开
收起
蛮大人123 2016-03-18 13:23:27 2577 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
     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);
        }
    2019-07-17 19:06:25
    赞同 展开评论
问答分类:
问答地址: