当前位置:网站首页>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 含头不含尾
边栏推荐
猜你喜欢
随机推荐
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
Teach you how to design a CSDN system
numpy中np.inf函数的用法讲解
240.搜索二维矩阵II
Qt在QTableWidget、View等表格中添加右击菜单
C语言(1)
MySQL stored procedure
多进程实现并发服务器
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
函数解剖——深挖getchar()与putchar()
navicat无法连接mysql超详细处理方法
安装pytorch
Record Breaker (Google Kickstart2020 Round D Problem A)
【C语言】三子棋(井字棋)的实现
208.实现Trie树(字典树与哈希表实现)
信号量解决生产者消费者问题
MySQL 灵魂 16 问,你能撑到第几问?
Teach you to completely uninstall MySQL
[GStreamer] The name of the plugin should match GST_PLUGIN_DEFINE
ezTrack-master使用教程