Java字符串查找

目录

1.查找字符

(1)以索引查找字符

(2)以字符查找索引

2.查找字符串


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

1.查找字符

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

(1)以索引查找字符

方法:

char charAt(int index)

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

java 复制代码
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

java 复制代码
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)

java 复制代码
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

java 复制代码
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

java 复制代码
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**

java 复制代码
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

java 复制代码
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

java 复制代码
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

java 复制代码
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
    }
}
相关推荐
宠友信息4 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
java1234_小锋4 小时前
Java高频面试题:Spring和SpringBoot的关系和区别?
java·spring boot·spring
风指引着方向4 小时前
昇腾算子性能调优:ops-nn 中的内存布局与向量化技巧
java·大数据·人工智能
WooaiJava4 小时前
流式TTS音频播放项目 - 面试问答(后端)
java·开发语言
奥升新能源平台5 小时前
奥升充电|充电站用户分层分析与精细化运营策略研究
java·大数据·能源
新缸中之脑5 小时前
开发AI代理必备的8个Python 库
开发语言·人工智能·python
暴走十八步5 小时前
PHP+vscode开启调试debug
开发语言·vscode·php
梵得儿SHI5 小时前
(第十篇)Spring AI 核心技术攻坚全梳理:企业级能力矩阵 + 四大技术栈攻坚 + 性能优化 Checklist + 实战项目预告
java·人工智能·spring·rag·企业级ai应用·springai技术体系·多模态和安全防护
一路向北⁢5 小时前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(三)
java·spring boot·后端·sse
郝学胜-神的一滴5 小时前
Python 列表 vs 数组:深入解析与最佳选择指南
开发语言·python·程序人生