当前位置:网站首页>[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
边栏推荐
- Find the sum of simple types of matrices
- 【精品】利用动态代理实现事务统一管理 二
- LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
- 【58】最后一个单词的长度【LeetCode】
- Use of Arthas in JVM tools
- Go语言自学系列 | golang方法
- Cadence process angle simulation, Monte Carlo simulation, PSRR
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- Multi view depth estimation by fusing single view depth probability with multi view geometry
- LLVM之父Chris Lattner:编译器的黄金时代
猜你喜欢
Idea import commons-logging-1.2 Jar package
Get the absolute path of the class according to the bytecode
Brief steps to build a website / application using flash and H5
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
bashdb下载安装
Pctp test experience sharing
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
L2-3 romantic silhouette (25 points)
PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集
随机推荐
Go语言自学系列 | golang嵌套结构体
ONEFLOW learning notes: from functor to opexprinter
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
OneFlow学习笔记:从Functor到OpExprInterpreter
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
2021李宏毅机器学习之Adaptive Learning Rate
请问中衍期货安全靠谱吗?
Taxable income
Go language self-study series | golang structure as function parameter
还原二叉树 (25 分)
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
Complete binary search tree (30 points)
Concave hull acquisition method based on convex hull of point cloud
Single chip microcomputer nixie tube stopwatch
Get the absolute path of the class according to the bytecode
资源打包关系依赖树
L2-023 图着色问题 (25 分)(图的遍历)
【58】最后一个单词的长度【LeetCode】
Latex mathematical formula
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing