JavaScript中的split()
方法用于將字符串分割為子字符串數組。該方法有兩種參數:
例如:
let str = "apple,banana,orange";
let fruits = str.split(","); // fruits will be ["apple", "banana", "orange"]
例如:
let str = "apple,banana,orange,grape";
let fruits = str.split(",", 2); // fruits will be ["apple", "banana"]
在這個例子中,split()
方法只返回了前兩個由逗號分隔的子字符串。