当前位置:网站首页>【IndexOf】【lastIndexOf】【split】【substring】用法详解
【IndexOf】【lastIndexOf】【split】【substring】用法详解
2022-04-23 08:18:00 【书启秋枫】
1. IndexOf() 和 lastIndexOf()的区别是什么?
indexOf() 和 lastIndexOf() 都是索引文件
indexOf() 是查某个指定的字符串在字符串首次出现的位置(索引值)(从左往右)
lastIndexOf() 是查某个指定的字符串在字符串最后一次出现的位置(索引值)(从左往右)
lastIndexOf() 方法是从后往前搜索,但返回的位置是还是从前开始数的。let arr = [1, 2, 4, 4, 5, 3, 3, 7, 8, 5]
console.log(arr.indexOf(4)); // 2
console.log(arr.lastIndexOf(4)); // 3
2. Java String indexOf() 方法
indexOf() 方法有以下四种形式:
public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
public class Main {
public static void main(String args[]) {
String string = "aaa456ac";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6
}
}
// 输出结果
// -1
// 6
// 7
// 7
// 6
// 6
3. Java ArrayList lastIndexOf() 方法
lastIndexOf() 方法返回指定元素在动态数组中最后一次出现的位置。
lastIndexOf() 方法的语法为:arraylist.lastIndexOf(Object obj)
class Main {
public static void main(String[] args){
// 创建一个数组
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Runoob");
System.out.println("网站列表: " + sites);
// 获取 Runoob 最后一次出现的位置
int position1 = sites.lastIndexOf("Runoob");
System.out.println("Runoob 最后出现的位置: " + position1);
// Wiki 不在 arraylist 中
// 返回 -1
int position2 = sites.lastIndexOf("Wiki");
System.out.println("Wiki 最后出现的位置: " + position2);
}
}
// 执行以上程序输出结果为:
// 网站列表: [Google, Runoob, Taobao, Runoob]
// Runoob 最后出现的位置: 3
// Wiki 最后出现的位置: -1
4. split() 方法根据匹配给定的正则表达式来拆分字符串
注意: . 、 $、 | 和 * 等转义字符,必须得加 \\。
注意:多个分隔符,可以用 | 作为连字符。
语法
public String[] split(String regex, int limit)
参数
regex -- 正则表达式分隔符。
limit -- 分割的份数。
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");
System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
以上程序执行结果为:
分隔符返回值 :
Welcome
to
Runoob分隔符设置分割份数返回值 :
Welcome
to-Runoob转义字符返回值 :
www
runoob
com多个分隔符返回值 :
acount=?
uu =?
n=?
5. String的substring()用于截取字符串
substring() 用于返回一个字符串的子字符串,即截取字符串功能。
substring()常用的重载方法如下:
substring(int beginIndex,int endIndex) 意思为返回下标从beginIndex开始(包括),到endIndex(不包括)结束的子字符串。
eg: String str = "http://www.oracle.com";
String subStr = str.substring(11, 17);
System.out.println(subStr); // oracle
substring(int beginIndex) 意思为返回下标从beginIndex开始(包括),到字符串结尾的子字符串。
eg: String str = "http://www.oracle.com";
String subStr = str.substring(7);
System.out.println(subStr); // www.oracle.com
版权声明
本文为[书启秋枫]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45037155/article/details/124356662
边栏推荐
- Enctype attribute in form
- Redis master-slave server problem
- 虚拟线上展会-线上vr展馆实现24h沉浸式看展
- CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
- Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
- 洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
- Detailed description of self feeling of auricular point weight loss 0422
- freertos学习02-队列 stream buffer message buffer
- 分组背包呀
猜你喜欢

增强现实技术是什么?能用在哪些地方?

My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together

K210学习笔记(二) K210与STM32进行串口通信

IDEA导入commons-logging-1.2.jar包
![[C语言] 文件操作《一》](/img/89/b19dda13d27e37fedf6736c102245b.png)
[C语言] 文件操作《一》

freertos学习02-队列 stream buffer message buffer

Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi

Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download

Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library

信息收集相关知识点及题解
随机推荐
pgsql想实现mysql一样样的列子查询操作
[learning] audio and video development from scratch (9) -- nuplayer
Data deletion and modification (MySQL)
PgSQL wants to implement all kinds of column sub query operations of MySQL
耳穴诊疗随笔0421
396. Rotate Function
关于数组复制问题
K210学习笔记(二) K210与STM32进行串口通信
ESP32程序下载失败,提示超时
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
耳穴减肥自身感受细节描述0422
Campus transfer second-hand market source code download
对li类数组对象随机添加特性,并进行排序
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Type anonyme (Principes fondamentaux du Guide c)
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
QT reading and writing XML files
DJ音乐管理软件Pioneer DJ rekordbox
让地球少些“碳”息 度能在路上