字符串方法
字符串方法
padStart()填充字符串之后
在原字符串开头填充指定的填充字符串直到目标长度所形成的新字符串
str.padStart(lenth);
str.padStart(lenth, strtxt);
1、lenth:当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。 2、strtxt(可选):填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的默认值为 " "。
'abc'.padStart(10); // " abc"
'abc'.padStart(10, "foo"); // "foofoofabc"
'abc'.padStart(6,"123465"); // "123abc"
'abc'.padStart(8, "0"); // "00000abc"
'abc'.padStart(1); // "abc"
padEnd() 填充字符串之前
str.padEnd(lenth);
str.padEnd(lenth, strtxt);
1、lenth:当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。 2、strtxt(可选):填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的缺省值为 " "。
'abc'.padEnd(10); // "abc "
'abc'.padEnd(10, "foo"); // "abcfoofoof"
'abc'.padEnd(6, "123456"); // "abc123"
'abc'.padEnd(1); // "abc"
删除字符串开头空白字符
trimStart() 方法用来删除字符串的开头的空白字符 trimLeft() 是它的别名 返回值:一个新的字符串,这个字符串左边的空格已经被去除掉了。
let str = ' a b cd ';
str.trimStart(); // 'a b cd '
str.trimLeft(); // 'a b cd '
删除字符串末尾空白字符
trimEnd() 方法用来删除字符串末尾的空白字符。 trimRight() 是它的别名 返回值:一个新的字符串,这个字符串右边的空格已经被去除掉了。
let str = ' a b cd ';
str.trimEnd(); // ' a b cd'
str.trimRight(); // ' a b cd'
charAt() 返回指定索引的字符
返回指定索引位置处的字符。类似于数组用中括号获取相应下标位置的数据。
var str = 'abcdefg'
console.log(str.charAt(2)) // 输出 'c'
console.log(str[2]) // 输出 'c'
charCodeAt() 返回指定索引的Unicode编码
strings.charCodeAt() 返回第N个字符的Unicode编码,为16位的整数。如下标越界,则返回NaN。
<script type="text/javascript">
let strings = "返回第N个字符。如下标越界,则返回空字符串";
console.log(strings.charCodeAt(3))
</script>
concat() 拼接字符串
类似数组的concat(),用来返回一个合并拼接两个或两个以上字符串。原字符串不变。
const str1 = 'abcdefg'
const str2 = '1234567'
const str3 = str1.concat(str2)
console.log(str3) // 输出 'abcdefg1234567'
indexOf() 查找字符串下标
返回一个字符在字符串中首次出现的位置
const str = 'abcdcefcg'
console.log(str.indexOf('c')) // 输出 '2'
console.log(str.lastIndexOf('c')) // 输出 '7'
lastIndexOf() 查找字符串下标
返回一个字符在字符串中最后一次出现的位置。
const str = 'abcdcefcg'
console.log(str.indexOf('c')) // 输出 '2'
console.log(str.lastIndexOf('c')) // 输出 '7'
split() 字符串转数组
案例一
var string = '111,222,333';
var stringRes = string.split(',');
console.log(stringRes) //输出["111", "222", "333"]
案例二
const str = 'A*B*C*D*E*F*G'
console.log(str.split('*')) // 输出 ["A", "B", "C", "D", "E", "F", "G"]
substr(), substring() 截取字符串
1、两个方法的功能都是截取一个字符串的片段,并返回截取的字符串。 2、substr和substring这两个方法不同的地方就在于参数二,substr的参数二是截取返回出来的这个字符串指定的长度,substring的参数二是截取返回这个字符串的结束点,并且不包含这个结束点。而它们的参数一,都是一样的功能,截取的起始位置。 3、注意事项:substr的参数二如果为0或者负数,则返回一个空字符串,如果未填入,则会截取到字符串的结尾去。substring的参数一和参数二为NAN或者负数,那么它将被替换为0。
const str = 'ABCDEFGHIJKLMN'
console.log(str.substr(2)) // 输出 'CDEFGHIJKLMN'
console.log(str.substring(2)) // 输出 'CDEFGHIJKLMN'
console.log(str.substr(2, 9)) // 输出 'CDEFGHIJK'
console.log(str.substring(2, 9)) // 输出 'CDEFGHI'
slice(start,end) 数组或字符串截断
从start 开始截取到end,但是不包括end,slice方法不会改变原始数组数组截断
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
console.log(citrus) //["Orange","Lemon"]
字符串截断
console.log('index.vue')
var fruits = 'abcdejdflsjfdls';
var citrus = fruits.slice(1,3);
console.log(citrus) // 'bc'
replace() 替换字符串
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
let strings = "返回子字符串第一个字符在原字符串中的下标";
console.log(strings.replace('第','DDDDDD'))
//返回子字符串DDDDDD一个字符在原字符串中的下标
toLowerCase() 字母转小写
把字母转换成小写
const str1 = 'abcdefg'
const str2 = 'ABCDEFG'
console.log(str2.toLowerCase()) // 输出:'abcdefg'
console.log(str1.toUpperCase()) // 输出:'ABCDEFG'
toUpperCase() 字母转大写
则是把字母转换成大写
const str1 = 'abcdefg'
const str2 = 'ABCDEFG'
console.log(str2.toLowerCase()) // 输出:'abcdefg'
console.log(str1.toUpperCase()) // 输出:'ABCDEFG'
首字母转大写或小写
利用slice()方法将字符串首字符分出来; 利用toUpperCase()方法将首字母转换为大写; 利用toLowerCase()方法将其他字符转换为小写; 利用“+”运算符将两个部分重新拼接起来即可。
includes(), startsWith(), endsWith()
includes、startsWith、endsWith,es6的新增方法 ,includes用来检测目标字符串对象是否包含某个字符,返回一个布尔值,startsWith用来检测当前字符是否是目标字符串的起始部分,相对的endwith是用来检测是否是目标字符串的结尾部分。
const str = 'Excuse me, how do I get to park road?'
console.log(str.includes('how')) // 输出:true
console.log(str.startsWith('Excuse')) // 输出: true
console.log(str.endsWith('?')) // 输出: true
repeat() 指定字符串重复次数
返回一个新的字符串对象,新字符串等于重复了指定次数的原始字符串。接收一个参数,就是指定重复的次数。原字符串不变。
const str = 'http'
const str2 = str.repeat(3)
console.log(str) // 输出:'http'
console.log(str2) // 输出:'httphttphttp'