当前位置:网站首页>[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
边栏推荐
- Go language self-study series | golang structure as function parameter
- uni-app和微信小程序中的getCurrentPages()
- Withholding agent
- Cadence process angle simulation, Monte Carlo simulation, PSRR
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
- 计算神经网络推理时间的正确方法
- Enterprise wechat application authorization / silent login
- 是否同一棵二叉搜索树 (25 分)
- BK3633 规格书
- Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
猜你喜欢
随机推荐
Research purpose, construction goal, construction significance, technological innovation, technological effect
Share the office and improve the settled experience
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Technological innovation in government affairs in the construction of Digital Government
MATLAB入门资料
Complete binary search tree (30 points)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
数字政府建设中政务中台中的技术创新点
Find the sum of simple types of matrices
Automatic differentiation and higher order derivative in deep learning framework
Go language self-study series | initialization of golang structure
是否完全二叉搜索树 (30 分)
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
Multi view depth estimation by fusing single view depth probability with multi view geometry
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
Summary of solid problems
Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing









