Java字符串查找

简介: Java字符串查找



在给定的字符串中查找需要的字符或字符串是常见的操作,以下是String类中常用的查找方法。

1.查找字符

查找字符分为两种情况:一种是根据索引查找该索引处的字符,另一种是根据给定的字符查找该字符的索引

(1)以索引查找字符

方法:

char charAt(int index)

该方法返回 index 位置上的字符,若 index 为负数或是越界,则抛出StringIndexOutOfBoundsException 异常

public class Test {
    public static void main(String[] args) {
        String str = "abcdefg";
        char ch = str.charAt(2);
        System.out.println(ch);//输出c
    }
}

(2)以字符查找索引

由于字符在字符串中可能出现多次,因此查找的方式不同,返回的索引也不相同,可以从前向后查找、从后向前查找,或是从指定位置开始查找

方法:

int indexOf(int ch)

从0索引开始找ch,返回ch第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "abcdefgaaa";
        int index1 = str.indexOf('a');
        int index2 = str.indexOf('m');
        System.out.println(index1);//输出0
        System.out.println(index2);//字符串中无字符m,找不到,返回-1.因此输出-1
    }
}

方法:

int lastIndexOf(int ch)

public class Test {
    public static void main(String[] args) {
        String str = "abcdefgaaa";
        int index = str.lastIndexOf('a');
        System.out.println(index);//输出9
    }
}

方法:

int indexOf(int ch, int formIndex)

fromIndex 位置开始找ch第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "abcdefgaaa";
        int index = str.indexOf('a',3);//从3索引位置开始找a
        System.out.println(index);//输出7
    }
}

方法:

int lastIndexOf(int ch, int fromIndex)

fromIndex 位置开始,向前找ch第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "abcdefgaaa";
        int index = str.lastIndexOf('a',8);//从8索引位置开始向前查找a
        System.out.println(index);//输出8
    }
}

2.查找字符串

由于字符串在指定的字符串中也可能出现多次,因此也可以从前向后查找、从后向前查找,或是从指定位置开始查找。

方法:

int indexOf(String str)

从0索引位置开始查找字符串str,返回 str 第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "aaabbbcccdedfg";
        String s = "abc";
        int index = str.indexOf(s);//字符串str中不包含abc,返回-1
        System.out.println(index);//输出-1
    }
}

方法:

int indexOf(String str, int fromIndex)

从fromIndex位置开始查找 str,返回 str  第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "aaabbbcccdedfg";
        String s = "bbc";
        int index = str.indexOf(s,3);
        System.out.println(index);//输出4
    }
}

方法:

int lastIndexOf(String str)

从后向前找,返回 str 第一次出现的位置,没有则返回-1

public class Test {
    public static void main(String[] args) {
        String str = "abcabcabc";
        String s = "abc";
        int index = str.lastIndexOf(s);
        System.out.println(index);//输出6
    }
}

方法:

int lastIndexOf(String str, int fromIndex)

从 fromIndex 位置开始向前查找 str,返回 str 第一次出现的位置,没有则返回 -1

public class Test {
    public static void main(String[] args) {
        String str = "abcabcabc";
        String s = "abc";
        int index = str.lastIndexOf(s,5);//从5索引位置开始向前查找s
        System.out.println(index);//输出3
    }
}
目录
相关文章
|
22天前
|
算法 Java
Java 有效字符串判断
Java 有效字符串判断
21 0
|
6天前
|
前端开发 JavaScript Java
【前端学java】详解java中的字符串操作(11)
【8月更文挑战第10天】详解java中的字符串操作
10 3
【前端学java】详解java中的字符串操作(11)
|
3天前
|
Java API 开发者
|
6天前
|
人工智能 Java 容器
十个Java字符串操作示例程序
十个Java字符串操作示例程序
6 1
|
8天前
|
存储 JavaScript Java
Java中未被初始化的字符串打印出“null”?
在Java中,未初始化的`String`变量默认值为`null`。打印此类变量时输出“null”,是因为`PrintStream`类中的`print`方法特别处理了`null`值,将其转换为字符串“null”。从JDK 17开始,`println`方法通过`String.valueOf`间接实现相同功能。当拼接包含`null`的字符串时,如`s1 + "BLACK"`,结果为“nullBLACK”,这是因为字符串构建过程中`StringBuilder`的`append`方法将`null`转换为“null”。
|
6天前
|
Java
Java中 字符串,字符串数组,整型数组初始化
Java中 字符串,字符串数组,整型数组初始化
6 0
|
1月前
|
Java
Java 替换字符串 replace replaceAll
【7月更文挑战第10天】Java 替换字符串 replace replaceAll
Java 替换字符串 replace  replaceAll
|
1月前
|
Java API 索引
Java中的字符串与字符操作详解
Java中的字符串与字符操作详解
|
2月前
|
Java 程序员
程序员必知:【java】判断字符串是否整数的三种方式,孰优孰劣请自行判断
程序员必知:【java】判断字符串是否整数的三种方式,孰优孰劣请自行判断
70 3
|
2月前
|
Java
java字符串分割split你用对了吗
java字符串分割split你用对了吗
20 1