博客
关于我
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/

你可能感兴趣的文章
Nacos编译报错NacosException: endpoint is blank
查看>>
nacos自动刷新配置
查看>>
nacos运行报错问题之一
查看>>
Nacos部署中的一些常见问题汇总
查看>>
NACOS部署,微服务框架之NACOS-单机、集群方式部署
查看>>
Nacos配置Mysql数据库
查看>>
Nacos配置中心中配置文件的创建、微服务读取nacos配置中心
查看>>
Nacos配置中心集群原理及源码分析
查看>>
nacos配置在代码中如何引用
查看>>
nacos配置新增不成功
查看>>
nacos配置自动刷新源码解析
查看>>
nacos集成分布式事务插件Seata的序列化问题,实际上是Seata本身存在bug!!
查看>>
Nacos集群搭建
查看>>
nacos集群搭建
查看>>
nacos集群网络分区对的影响和运维方式
查看>>
nacos集群节点故障对应用的影响以及应急方法
查看>>
nacos集群配置详解
查看>>
Nagios 3.0 Jumpstart Guide For Linux – Overview, Installation and Configuration
查看>>
nagios 实时监控 iptables 状态
查看>>
WAP短信格式解析及在Linux下用C语言实现
查看>>