• Java字符串查找


    目录

    1.查找字符

    (1)以索引查找字符

     (2)以字符查找索引

    2.查找字符串


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

    1.查找字符

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

    (1)以索引查找字符

    方法:

    char charAt(int index)

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

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

     

     (2)以字符查找索引

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

    方法:

    int indexOf(int ch)

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

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

    方法:

    int lastIndexOf(int ch)

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

    方法:

    int indexOf(int ch, int formIndex)

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

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

    方法:

     int lastIndexOf(int ch, int fromIndex)

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

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

    2.查找字符串

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

    方法:

    int indexOf(String str)

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

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

    方法:

    int indexOf(String str, int fromIndex)

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

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

    方法:

    int lastIndexOf(String str)

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

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

     方法:

    int lastIndexOf(String str, int fromIndex)

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

    1. public class Test {
    2. public static void main(String[] args) {
    3. String str = "abcabcabc";
    4. String s = "abc";
    5. int index = str.lastIndexOf(s,5);//从5索引位置开始向前查找s
    6. System.out.println(index);//输出3
    7. }
    8. }

  • 相关阅读:
    PostgreSQL基本操作
    ‘==‘与‘=‘并非胖与瘦一样容易分辨
    R语言使用plot函数可视化数据散点图,通过axis函数添加X轴坐标轴(add x axis)
    【深度学习 论文篇 01-1 】AlexNet论文翻译
    Java项目:SSM邮件收发管理系统
    Express
    python3 flask 实现对config.yaml文件的内容的增删改查,并重启服务
    DSA之排序(2):插入排序
    E - Notebook(tree&map)
    CSS样式怎么实现圆角矩形功能
  • 原文地址:https://blog.csdn.net/2301_76161469/article/details/132710105