"景先生毕设|www.jxszl.com

indexOf()用法

2023-09-12 15:40编辑: www.jxszl.com景先生毕设
                           indexOf()用法
定义:
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。 如果没有找到子字符串,则返回-1。
用法:
1.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2.int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3.int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4.int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

代码示例:
public class Test { 
    public static void main(String[] args) { 
      let str = 'orange';
 
      System.out.println(str.indexOf('o')); //0
      System.out.println(str.indexOf('n')); //3
      System.out.println(str.indexOf('c')); //-1
    } 

原文链接:http://www.jxszl.com/biancheng/JAVA/446466.html