// ==============字符串方法或属性================
// 所有字符串方法都会返回新字符串。它们不会修改原始字符串。
// 正式地说:字符串是不可变的:字符串不能更改,只能替换。
/*
length:该属性返回字符串的长度
indexOf:该方法返回字符串中指定文本首次出现的索引(位置)、未找到文本,则返回-1 (可设置检索的开始位置)
lastIndexOf:该方法返回字符串中指定文本最后一次出现的索引(位置)、未找到文本,则返回-1(该方法是从尾到头检索,检索开始位置从尾部开始计算)
search:该方法返回字符串中指定文本首次出现的位置(可设置更强大的检索值:正则表达式)
slice:该方法提取字符串的某个部分,并返回该部分,该方法设置两个参数:起始索引,结束索引,(索引为负,则从数组尾部开始)
subString:该方法跟slice类似,只是无法接受负值索引
substr:该方法与slice类似,只是第二个参数是:提取的长度
replace:该方法把字符串中指定的值,用另一个值替换,(以新字符串返回,只替换首个匹配,大小写敏感,支持用正则)
toUpperCase: 该方法把字符串转化为大写
toLowerCase: 该方法把字符串转化为小写
concat:连接两个或多个字符串
trim:该方法去除字符串的两端空白符
charAt:该方法是提取字符串字符的安全方法,返回字符串中指定下标的字符串
charCodeAt: 该方法是提取字符串字符的安全方法,返回字符串中指定下标的字符串的unicode编码
split:该方法是可以将字符串转化为数组
*/
// 字符串:
setString('split');
function setString(eventName) {
let str = 'hello word!';
switch (eventName) {
case 'length':
console.log('length属性返回长度:', str.length); // length属性返回长度: 11
break;
case 'indexOf':
console.log('indexOf方法查询成功,返回下标:', str.indexOf('!')); // indexOf方法查询成功,返回下标: 10
console.log('indexOf方法未查询到返回:', str.indexOf('!hello')); // indexOf方法未查询到返回: -1
break;
case 'lastIndexOf':
console.log(
'lastIndexOf方法查询成功:返回最后一次出现的位置:',
str.lastIndexOf('word')
); //lastIndexOf方法查询成功:返回最后一次出现的位置: 6
console.log('lastIndexOf方法未查询到返回:', str.lastIndexOf('!word')); // lastIndexOf方法未查询到返回: -1
break;
case 'search':
console.log('search方法查询成功,返回位置下标', str.search('!')); // search方法查询成功,返回位置下标 10
console.log('search方法查询失败,返回:', str.search('!word')); // search方法查询失败,返回: -1
console.log('search方法支持用正则查询:', str.search(/\s/)); // search方法支持用正则查询: 5
break;
case 'slice':
console.log('slice方法提取:', str.slice(1, 3)); // slice方法提取: el
break;
case 'substring':
console.log('substring方法提取:', str.substring(1, 3)); // substring方法提取: el
console.log('substring方法无法接受负索引:', str.substring(-1, 3)); // substring方法无法接受负索引: hel
break;
case 'substr':
console.log('substr方法第二个参数是提取长度:', str.substr(2, 3)); // substr方法第二个参数是提取长度: llo
break;
case 'replace':
console.log(
'replace方法用于替换字符串中指定的值为指定的值:',
str.replace(' ', ' gansong ')
); // replace方法用于替换字符串中指定的值为指定的值: hello gansong word!
break;
case 'toUpperCase':
console.log('toUpperCase用于字符串转大写:', str.toUpperCase()); // toUpperCase用于字符串转大写: HELLO WORD!
break;
case 'toLowerCase':
console.log(
'toLowerCase用于字符串转小写:',
str.toUpperCase().toLowerCase()
); // toLowerCase用于字符串转小写: hello word!
break;
case 'concat':
let concatStr1 = '1',
concatStr2 = '2';
console.log(
'concat方法用于连接两个或多个字符串:',
str.concat(concatStr1, concatStr2, ' !') // concat方法用于连接两个或多个字符串: hello word!12 !
);
break;
case 'trim':
let trimStr = ' tr im ';
console.log('trim方法用于去除字符串两端空格:', trimStr.trim()); // trim方法用于去除字符串两端空格: tr im
break;
case 'charAt':
console.log(
'charat方法是提取字符串中指定下标的安全方法,返回字符串:',
str.charAt(4)
); // charat方法是提取字符串中指定下标的安全方法,返回字符串: o
case 'charCodeAt':
console.log(
'charCodeAt方法是提取字符串指定下标的安全方法,返回unicode编码:',
str.charCodeAt(0)
); // charCodeAt方法是提取字符串指定下标的安全方法,返回unicode编码: 104
break;
case 'split':
console.log('split可以把字符串转化为数组:', str.split('')); // split可以把字符串转化为数组: (11) ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd', '!']
}
}
js 字符串方法
最新推荐文章于 2024-01-08 09:00:00 发布