做相冊本哪個網(wǎng)站好用嗎短視頻推廣
目錄
查找字符串中的特定元素 String.indexOf()? ? ? ? (返回索引值)
截取字符串的一部分 .substring()? ? ? ? (不影響原數(shù)組)(不允許負值)
截取字符串的一部分 .slice()? ? ? ? (不影響原數(shù)組)(允許負值)
字符串的分段 .split()? ? ? ? (字符串轉(zhuǎn)數(shù)組)(不影響原數(shù)組)
字符串是否以什么開頭/結(jié)尾 .startsWith() .endsWith()
后續(xù)會更新
查找字符串中的特定元素 String.indexOf()? ? ? ? (返回索引值)
? ? ? ? String.indexOf(value,start)? ? ? ? (起始位start可選)
const a = "Blue Whale";
console.log(a.indexOf("Blue"))//0
console.log(a.indexOf(""));//0
console.log(a.indexOf("",14));//10
//當查找的為空字符時,起始位超過字符串本身則返回字符串長度
console.log(a.indexOf("ppop"));//-1
截取字符串的一部分 .substring()? ? ? ? (不影響原數(shù)組)(不允許負值)
? ? ? ? String.substring(startIndex,endIndex)? ? ?(endIndex可選,不寫默認為最后)(取左不取右)
const str = 'Mozilla';
console.log(str.substring(1, 3));
//oz
console.log(str.substring(2));
//zilla
截取字符串的一部分 .slice()? ? ? ? (不影響原數(shù)組)(允許負值)
? ? ? ? String.slice(startIndex,endIndex)? ? ? ? (同上)
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
//the lazy dog.
console.log(str.slice(4, 19));
//quick brown fox
字符串的分段 .split()? ? ? ? (字符串轉(zhuǎn)數(shù)組)(不影響原數(shù)組)
? ? ? ? String.split(value)? ? ? ? (返回數(shù)組)(value可選)
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);
//fox
const chars = str.split('');
console.log(chars[8]);
//k
const strCopy = str.split();
console.log(strCopy);
//["The quick brown fox jumps over the lazy dog."]
字符串是否以什么開頭/結(jié)尾 .startsWith() .endsWith()
? ? ? ? String.startsWith(value,[startIndex])? ? ? ? (返回布爾值)(不允許負值)
????????String.endsWith(value,[endIndex])?
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// true
console.log(str1.startsWith('Sat', 3));
// false
console.log(str1.endsWith('plans'));
// true
console.log(str1.endsWith('plan', 19));
// true