1. 程式人生 > >js 字符串拼接、截取、查找...

js 字符串拼接、截取、查找...

scrip bsp 分割 arc 組合 substring console light min

函數:split()
功能:使用一個指定的分隔符把一個字符串分割存儲到數組
例子:

let str=”020-88888888-03”; 
let arr=str.split(”-”); 

console.log(arr);
//arr是一個包含字符值”020”、”88888888”、”03”的數組 。

函數:Join()
功能:使用您選擇的分隔符將一個數組合並為一個字符串 ;

ler arr = [‘1‘,‘2‘,‘3‘,‘4‘,‘5‘];

let str = arr.join(‘,‘);

console.log(str);

//str就是 1,2,3,4,5 的字符串。

  

函數:substring()
功能:字符串截取。

例如

let str ="MinidxSearchEngine”;
let i =‘‘;
i = str.substring(0,6);
console.log(i);
//  ‘Minidx‘

  

函數:indexOf()
功能:返回字符串中匹配子串的第一個字符的下標 ,如果沒有則返回-1。

let myString=”JavaScript”; 
let a=myString.indexOf(”v”);
// 2 
let b=myString.indexOf(”S”);
// 4 
let c = myString.indexOf(”b”);
// -1

函數:concat() 或者 "+" 或者ES6的 "`"(小撇號)
功能:字符串拼接。  

let a = ‘ab‘;
let b = ‘cd‘;
a.concat(b);
// ‘abcd‘;
a + b;
// ‘abcd‘;
`${a}${b}`;
// ‘abcd‘

  

js 字符串拼接、截取、查找...