1. split: Split a string into substrings and return the result as a string array.
Example 1:
String str="Java string split test";
String[] strarray=str.split(" ");
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
将输出:
Java
string
split
test
Example 2:
String str="Java string split test";
String[] strarray=str.split(" ",2);//使用limit,最多分割成2个字符串
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
将输出:
Java
string split test
Example 3:
String str="192.168.0.1";
String[] strarray=str.split(".");
for (int i = 0; i < strarray.length; i++)
System.out.println(strarray[i]);
结果是什么也没输出,将split(".")改为split("\\."),将输出正确结果:
192
168
0
1
2. indexOf(): Returns the position where a specified string value first appears in the string (from left to right). If there is no match, -1 is returned, otherwise the subscript value of the string at which the first occurrence occurs is returned.
Example 1:
let str = 'apple';
str.indexOf('a'); //0
str.indexOf('l'); //3
let str='2016';
str.indexOf(2); //0
str.indexOf('1'); //0
//说明indexOf会对字符串内的数字做简单的类型转换,把数字2转换成字符串'2'然后执行。
Example 2:
//分割接口'http'用
let dell = []
let str = this.array[index].voices[inde]
// 分割字符串
if (this.array[index].voices[inde].indexOf('http') == 0) {
dell.push(this.array[index].voices[inde].substring(21, this.array[index].voices[inde].length))}
console.log(dell);
innerAudioContext.src = 'https://new.kuxia.top' + dell;
3. substr(start,length): Indicates that starting from the start position, intercept the length-length string.
4. substring(start,end): represents the string from start to end, including the characters at the start position but excluding the characters at the end position.
Example 1:
public static void main(String[] args){
String test = "Hello World !";
String subTest1 = test.substring(0,3);
System.out.println("subTest:" + subTest1);
String subTest2 = test.substring(0,test.length());
System.out.println("subTest:" + subTest2);
}
运行结果如下:
subTest:Hel
subTest:Hello World !
5. lastIndexOf(): The method returns the index value of the first character where a certain character or string appears from right to left (opposite of indexOf)
Example:
let str = "Hello world!"
console.log(str.lastIndexOf("Hello"))//0
console.log(str.lastIndexOf("World"))//-1
console.log(str.lastIndexOf("world"))//6