当前位置:网站首页>[indexof] [lastIndexOf] [split] [substring] usage details
[indexof] [lastIndexOf] [split] [substring] usage details
2022-04-23 08:52:00 【Book opens autumn maple】
1. IndexOf() and lastIndexOf() What's the difference ?
indexOf() and lastIndexOf() It's all index files
indexOf() Is to check the first occurrence of a specified string in the string ( Index value )( From left to right )
lastIndexOf() Is to check the position of a specified string at the last occurrence of the string ( Index value )( From left to right )
lastIndexOf() The method is to search from back to front , But the returned position is the same as before .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() Method
indexOf() The method has the following four forms :
public int indexOf(int ch): Returns the index of the first occurrence of the specified character in the string , If there is no such character in this string , Then return to -1.
public int indexOf(int ch, int fromIndex): Return from fromIndex Position starts to find the index of the first occurrence of the specified character in the string , If there is no such character in this string , Then return to -1.
int indexOf(String str): Returns the index of the first occurrence of the specified character in the string , If there is no such character in this string , Then return to -1.
int indexOf(String str, int fromIndex): Return from fromIndex Position starts to find the index of the first occurrence of the specified character in the string , If there is no such character in this string , Then return to -1.
public class Main {
public static void main(String args[]) {
String string = "aaa456ac";
// Finds the subscript where the specified character is in the string . In, it returns the subscript of the string ; If not, return to -1.
System.out.println(string.indexOf("b")); // indexOf(String str); Return results :-1,"b" non-existent
// Start at the fourth character position and continue to find , Contains the current location
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); Return results :6
//( The difference from before : The above parameter is String type , The following parameters are int type ) Reference data :a-97,b-98,c-99
// Find whether the specified character exists from the beginning
System.out.println(string.indexOf(99));//indexOf(int ch); Return results :7
System.out.println(string.indexOf('c'));//indexOf(int ch); Return results :7
// from fromIndex lookup ch, This is a character variable , It's not a string . character a The corresponding number is 97.
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); Return results :6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); Return results :6
}
}
// Output results
// -1
// 6
// 7
// 7
// 6
// 6
3. Java ArrayList lastIndexOf() Method
lastIndexOf() Method returns the position of the last occurrence of the specified element in the dynamic array .
lastIndexOf() The syntax of the method is :arraylist.lastIndexOf(Object obj)
class Main {
public static void main(String[] args){
// Create an array
ArrayList<String> sites = new ArrayList<>();
sites.add("Google");
sites.add("Runoob");
sites.add("Taobao");
sites.add("Runoob");
System.out.println(" Website list : " + sites);
// obtain Runoob The last location
int position1 = sites.lastIndexOf("Runoob");
System.out.println("Runoob The last place : " + position1);
// Wiki be not in arraylist in
// return -1
int position2 = sites.lastIndexOf("Wiki");
System.out.println("Wiki The last place : " + position2);
}
}
// The output of the above program is :
// Website list : [Google, Runoob, Taobao, Runoob]
// Runoob The last place : 3
// Wiki The last place : -1
4. split() Method splits the string by matching the given regular expression
Be careful : . 、 $、 | and * Wait for escape characters , Must add \\.
Be careful : Multiple separators , It can be used | As a hyphen .
grammar
public String[] split(String regex, int limit)
Parameters
regex -- Regular expression separator .
limit -- The number of split copies .
public class Test {
public static void main(String args[]) {
String str = new String("Welcome-to-Runoob");
System.out.println("- The separator returns the value :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
System.out.println("- The separator sets the number of splits and the return value :" );
for (String retval: str.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.runoob.com");
System.out.println(" Escape character return value :" );
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(" Multiple separator return values :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
The execution result of the above procedure is :
The separator returns the value :
Welcome
to
RunoobThe separator sets the number of splits and the return value :
Welcome
to-RunoobEscape character return value :
www
runoob
comMultiple separator return values :
acount=?
uu =?
n=?
5. String Of substring() Used to intercept strings
substring() Substring used to return a string , That is, the function of intercepting strings .
substring() Common overloading methods are as follows :
substring(int beginIndex,int endIndex) Return subscript from beginIndex Start ( Include ), To endIndex( barring ) The ending substring .
eg: String str = "http://www.oracle.com";
String subStr = str.substring(11, 17);
System.out.println(subStr); // oracle
substring(int beginIndex) Return subscript from beginIndex Start ( Include ), Substring to the end of the string .
eg: String str = "http://www.oracle.com";
String subStr = str.substring(7);
System.out.println(subStr); // www.oracle.com
版权声明
本文为[Book opens autumn maple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230818433496.html
边栏推荐
- Correct method of calculating inference time of neural network
- PCTP考试经验分享
- rembg 分割mask
- php基于哈希算法出现的强弱比较漏洞
- 怎样读取Excel表格到数据库
- Idea package jar file
- Output first order traversal according to second order and middle order traversal (25 points)
- Introduction to matlab
- STM32F103ZET6【标准库函数开发】----库函数介绍
- Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
猜你喜欢
idea底栏打开services
K210 learning notes (II) serial communication between k210 and stm32
After a circle, I sorted out this set of interview questions..
Cadence process angle simulation, Monte Carlo simulation, PSRR
引用传递1
MATLAB入门资料
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
MySQL查询两张表属性值非重复的数据
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
随机推荐
L2-022 rearrange linked list (25 points) (map + structure simulation)
idea打包 jar文件
GUI编程简介 swing
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
Share the office and improve the settled experience
Consensus Token:web3. 0 super entrance of ecological flow
Go语言自学系列 | golang结构体作为函数参数
应纳税所得额
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
LLVM之父Chris Lattner:编译器的黄金时代
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
资源打包关系依赖树
rembg 分割mask
After a circle, I sorted out this set of interview questions..
Go语言自学系列 | golang结构体的初始化
2021李宏毅机器学习之Adaptive Learning Rate
LaTeX论文排版操作
Consensus Token:web3.0生态流量的超级入口