【IndexOf】【lastIndexOf】【split】【substring】用法详解

1. IndexOf() 和 lastIndexOf()的区别是什么? 

indexOf() 和 lastIndexOf() 都是索引文件

indexOf() 是查某个指定的字符串在字符串首次出现的位置(索引值)(从左往右)

lastIndexOf() 是查某个指定的字符串在字符串最后一次出现的位置(索引值)(从左往右)
lastIndexOf() 方法是从后往前搜索,但返回的位置是还是从前开始数的。

let arr = [1, 2, 4, 4, 5, 3, 3, 7, 8, 5]

   console.log(arr.indexOf(4));  // 2
   console.log(arr.lastIndexOf(4));  // 3

2. Java String indexOf() 方法

indexOf() 方法有以下四种形式:

  • 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。

public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}


// 输出结果
// -1
// 6
// 7
// 7
// 6
// 6

3. Java ArrayList lastIndexOf() 方法 

lastIndexOf() 方法返回指定元素在动态数组中最后一次出现的位置。

lastIndexOf() 方法的语法为:arraylist.lastIndexOf(Object obj)

class Main {
    public static void main(String[] args){

        // 创建一个数组
        ArrayList<String> sites = new ArrayList<>();

        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Runoob");
        System.out.println("网站列表: " + sites);


        // 获取 Runoob 最后一次出现的位置
        int position1 = sites.lastIndexOf("Runoob");
        System.out.println("Runoob 最后出现的位置: " + position1);

        // Wiki 不在 arraylist 中
        // 返回 -1
        int position2 = sites.lastIndexOf("Wiki");
        System.out.println("Wiki 最后出现的位置: " + position2);
    }
}

// 执行以上程序输出结果为:

// 网站列表: [Google, Runoob, Taobao, Runoob]
// Runoob 最后出现的位置: 3
// Wiki 最后出现的位置: -1

4. split() 方法根据匹配给定的正则表达式来拆分字符串

注意: . 、 $、 | 和 * 等转义字符,必须得加 \\。

注意:多个分隔符,可以用 | 作为连字符。

语法

public String[] split(String regex, int limit)

参数

  • regex -- 正则表达式分隔符。

  • limit -- 分割的份数。

public class Test {
    public static void main(String args[]) {
        String str = new String("Welcome-to-Runoob");
 
        System.out.println("- 分隔符返回值 :" );
        for (String retval: str.split("-")){
            System.out.println(retval);
        }
 
        System.out.println("");
        System.out.println("- 分隔符设置分割份数返回值 :" );
        for (String retval: str.split("-", 2)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str2 = new String("www.runoob.com");
        System.out.println("转义字符返回值 :" );
        for (String retval: str2.split("\\.", 3)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str3 = new String("acount=? and uu =? or n=?");
        System.out.println("多个分隔符返回值 :" );
        for (String retval: str3.split("and|or")){
            System.out.println(retval);
        }
    }
}

以上程序执行结果为:

分隔符返回值 :
        Welcome
        to
        Runoob

分隔符设置分割份数返回值 :
        Welcome
        to-Runoob

转义字符返回值 :
        www
        runoob
        com

多个分隔符返回值 :
        acount=?
        uu =?
        n=?

5. String的substring()用于截取字符串

substring() 用于返回一个字符串的子字符串,即截取字符串功能。

substring()常用的重载方法如下:

substring(int beginIndex,int endIndex) 意思为返回下标从beginIndex开始(包括),到endIndex(不包括)结束的子字符串。

               eg: String str = "http://www.oracle.com";

                       String subStr = str.substring(11, 17);

                       System.out.println(subStr); // oracle

substring(int beginIndex) 意思为返回下标从beginIndex开始(包括),到字符串结尾的子字符串。

             eg: String str = "http://www.oracle.com";

                       String subStr = str.substring(7);

                      System.out.println(subStr); // www.oracle.com

猜你喜欢

转载自blog.csdn.net/qq_45037155/article/details/124356662