博客
关于我
Java String indexOf() 方法
阅读量:282 次
发布时间:2019-03-01

本文共 942 字,大约阅读时间需要 3 分钟。

  • public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str): 返回指定子串在字符串中第一次出现处的索引,如果此字符串中没有这样的子串,则返回 -1,有则返回该子串在字符串的第一次出现的子串第一个字符的下标。

  • int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1,有则返回该子串在字符串的从指定fromIndex 位置开始出现的子串第一个字符的下标。

/*     *     * 需求把第二次出现[template]的换成A     *     */    public static void main(String[] args) {         String str = "3187381718314391814718321493018";      String template = "18";      StringBuilder stringBuilder = new StringBuilder(str);      // 从指定位置开始查找子串,返回子串的第一个元素的下标      int indexZero = str.indexOf(template);      int indexFirst = str.indexOf(template,indexZero + template.length());      stringBuilder.replace(indexFirst,indexFirst + template.length(),"A");      System.out.println(stringBuilder.toString());    }

转载地址:http://oaka.baihongyu.com/

你可能感兴趣的文章
vue样式穿透 ::v-deep的具体使用
查看>>
Netty工作笔记0065---WebSocket长连接开发4
查看>>
Netty工作笔记0066---Netty核心模块内容梳理
查看>>
Vue基本使用---vue工作笔记0002
查看>>
Netty工作笔记0068---Protobuf机制简述
查看>>
Netty工作笔记0069---Protobuf使用案例
查看>>
Netty工作笔记0070---Protobuf使用案例Codec使用
查看>>
Netty工作笔记0071---Protobuf传输多种类型
查看>>
Netty工作笔记0072---Protobuf内容小结
查看>>
Netty工作笔记0073---Neety的出站和入站机制
查看>>
Netty工作笔记0074---handler链调用机制实例1
查看>>
Netty工作笔记0075---handler链调用机制实例1
查看>>
Netty工作笔记0076---handler链调用机制实例3
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0078---Netty其他常用编解码器
查看>>
Netty工作笔记0079---Log4j整合到Netty
查看>>
Netty工作笔记0080---编解码器和处理器链梳理
查看>>
Netty工作笔记0081---编解码器和处理器链梳理
查看>>
Netty工作笔记0082---TCP粘包拆包实例演示
查看>>
Netty工作笔记0083---通过自定义协议解决粘包拆包问题1
查看>>