1. slice()
slice(startIndex,endIndex)
2. substr()
substr(index,length)
3. substring()
substring(startIndex,endIndex)
var arr = "hello"
console.log(arr.slice(0)); // hello
console.log(arr.slice(0,3)); // hel
console.log(arr.substr(0,3)); // hel
console.log(arr.substring(0,2)); // he
4. charAt()
charAt(index) 根据下标查找对应的值
var str = "hello"
console.log(str.charAt(1)); // e
5. indexOf(value)
根据值查找对应的下标 找不到返回-1
var arr = "hello";
console.log(arr.indexOf('e')); // 1
6. search(value)
根据值查找对应的下标 找不到返回-1
var str = "你是谁"
var index = str.search("她")
console.log(index); // -1
7. includes
是否包含某位(多位)字符 返回boolean
var arr = "hello"
console.log(arr.includes("eh")); //false
8. match(value)
返回匹配的字符串,返回的是数组
var str ="hello"
var arr = str.match("l")
console.log(arr); // ["l", index: 2, input: "hello", groups: undefined]
// 找不到返回 null
9. length
字符串的长度
var str = "hello"
console.log(str.length);
var s = "故事的结尾,心上人"
console.log(handleStr(s));
function handleStr(value){
if(value.length>5){
return value.slice(0,5)+"..."
}
return value
}