当前位置:网站首页>String类型字符串获取第一次或者最后一次出现的下标
String类型字符串获取第一次或者最后一次出现的下标
2022-07-30 05:39:00 【北木桥溪】
1、获取下标
String S = "0123456789 0123456789 0123456789";
System.out.println(S.indexOf("23"));//输出2 (代表第一次出现字符串“23”的下标位置为2)
System.out.println(S.indexOf("23", 4));//输出13 (代表从下标位置4开始,第一次出现字符串“23”的下标位置为13)
System.out.println(S.lastIndexOf("89"));//输出30 (代表最后一次出现字符串“89”的下标位置为30)
System.out.println(S.lastIndexOf("23",11));//输出2 (代表从下标的位置0开始到下标的位置11结束最后一次出现字符串“23”的下标位置为2)
2、截取字符
substring(int beginIndex) 返回从起始位置到字符串末尾
substring(int beginIndex, int endIndex) 返回从起始位置到目标位置之间的字符串
但不包含目标位置
String a="0123456789";
System.out.println(a.substring(5));//输出56789 含头含尾
System.out.println(a.substring(5,9));//输出5678 含头不含尾
边栏推荐
- 相对路径和绝对路径的区别
- 安装Nuxt.js时出现错误:TypeError:Cannot read property ‘eslint‘ of undefined
- Record Breaker (Google Kickstart2020 Round D Problem A)
- navicat无法连接mysql超详细处理方法
- 人生的第一篇博客(初识代码)
- “tensorflow.keras.preprocessing“ has no attribute “image_dataset_from_directory“
- 进程间的通信方式简介
- 函数解剖——深挖printf()与scanf()
- Frobenius norm(Frobenius 范数)
- 安装pytorch
猜你喜欢
随机推荐
面试前需要巩固的算法知识点(自用,更新中)
Qt设置窗口可拖动
【线性神经网络】线性回归 / 基础优化方法
CISP-PTE Zhenti Demonstration
三子棋游戏——C语言
torch.load()
np.argsort()函数详细解析
Navicat new database
2022 SQL big factory high-frequency practical interview questions (detailed analysis)
安装pytorch
Countdown (Source: Google Kickstart2020 Round C Problem A) (DAY 88)
猜数字小游戏(随机生成’三剑客‘)
51.N皇后(回溯法)
C语言指针(指针数组、数组指针、函数指针、传参、回调函数等)超详细
‘kaggle视频游戏销售数据的可视化和分析‘项目实现
934.最短的桥(广度优先搜索)
0基础玩转C语言—初识C语言(上)
MySQL stored procedure
MySQL database basics (a systematic introduction)
How is crawler data collected and organized?









