一、字符串方法
-
concat(str2,[str3…]) 用于把两个或多个字符串组合起来,返回一个新的字符串
-
使用str1.concat(str2,str3…)
- concat 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
- concat 方法并不 影响原字符串。
- 如果参数不是字符串类型,它们在连接之前将会被转换成字符串。
var str1='hello' var str2='world' var str3='Vue' var str4='React' var str=str1.concat(str2,str3,str4) console.log(str1,'1'); console.log(str); // helloworldVueReact let strArr=['hello',' ','world'] console.log(...strArr); console.log(''.concat([])); // '' console.log(''.concat({})); // [object Object] console.log(''.concat(null)); // null console.log(''.concat(true)); // true console.log(''.concat(4,5)); // 45
-
-
indexOf() 返回字符串中第一个字串出现的第一个索引。如果匹配不到返回-1
- str.indexOf(searchValue [, 可选fromIndex] 第一个参数 字符,第二个参数 开始位置
- searchValue 要被查找的字符串值。如果没有提供确切地提供字符串 ,会自动转为undefined,然而在当前字符串查找这个值。‘undefined’.indexOf() 返回0 而’undefine’.indexOf返回-1
- fromIndex可选值默认是0,如果formIndex的值小于0或者大于str.lenght,那么分别从0处和str.length开始找
- 返回值,如果查找的是一个空串,会产生奇怪的结果,如果起始索引为空,或者起始索引fromindex的值小于被查字符串的长度,返回值和fromindex一一致
- 检测字符是否存在 ‘blue red yellow’.indexOf(‘blue’)!==-1);
- indexOf区分大小写
var str='abcdef' var index=str.indexOf('d',0) console.log(index); // undefined console.log('undefined'.indexOf()); console.log('undefine'.indexOf()); // 起始位置小于0或者大于str.length console.log(str.indexOf('b',-1)); console.log(str.indexOf('b',11)); // searchValue为空 且起始位置小于str.length 返回索引 console.log(str.indexOf(''));//0 console.log(str.indexOf('',2));//2 console.log(str.indexOf('',3));//3 console.log(str.indexOf('',5));//5 // searchValue为空 且起始位置小于str.length 返回长度 console.log(str.indexOf('',12));//6 console.log(str.indexOf('',23));//6 console.log(str.indexOf('',35));//6 console.log('blue red yellow'.indexOf('blue')!==-1);//true
- 统计某一字符出现的次数
var str='adkfjladadakdl' var count=0 var pros=str.indexOf('a') while(pros!==-1) { count++ pros=str.indexOf('a',pros+1) } console.log(count);
-
chartAt() 返回指定位置的字符 根据下标获取字符
var str='abcdef' console.log(str.charAt(2));
-
lastIndexOf() 返回字符串字串出现的最后一处出现的位置索引 没有匹配的话返回-1
-
str.lastIndexOf(searchValue[, fromIndex])
-
一个字符串,表示被查找的值。如果
searchValue
是空字符串,则返回fromIndex
。 -
用法和indexOf差不多
-
-
substr() 返回起始位置startPos位置,长度为length的字符串
- str.substr(start[, length]) 起始位置 截取个数
- start 开始索引是0 结束索引是length-1
- 如果开始字符为正值,且大于字符串的长度,返回一个空字符串
- 如果起始位置为负值,作为字符串尾部开始的字符索引,如果start为负值,且绝对值大于字符长度,则substr使用0作为开始提取的索引
- 如果截取长度为0或者负值则返回一个空字符串
let str='helloWorld' console.log(str.substr(0,2)); // he console.log(str.substr(-1)); // d console.log(str.substr(-5,2)); // Wo console.log(str.substr()); // helloWorld console.log(str.substr(12)); // '' console.log(str.substr(-12)); // helloWorld console.log(str.substr(0)); // helloWorld
-
substring() 返回字符串的一个字串,传入的是起始位置和结束位置
- str.substring(indexStart[, indexEnd]) 起始索引
- 如果起始位置值相同返回一个空字符串
- 如果省略这两个参数,提取字符一直到末尾
- 如果参数数值小于0或为NaN则看做为0
- 如果任意参数大于str.length 则是str.length
- 如果startIndex大于endIndex则二者调换位置
- 使用length属性截取倒数指定字符串的倒数元素 substring(str.length-4)
let str='helloWorld' console.log(str.substring(0,5)); console.log(str.substring(5,0)); console.log(str.substring(5,NaN)); // 截取字符最后的字符 console.log(str.substring(str.length-1));
-
slice() 提取字符的一部分,返回一个新字符 不会改变原字符串
- str.slice(beginIndex[, endIndex])
- 参数代表起始位置和结束位置
- 当值传递一个负数时,会结尾开始,截取到结尾
- 负值大于总长度,当做startIndex=0看待
- 当传递一个参数时,没有要求,传递两个参数时,第二个参数不包含
var str='helloWorld' console.log(str.slice(0,5)); // hello console.log(str.slice(-3)); // rld console.log(str.slice(-50)); // helloWorld console.log(str.slice(0,-1)); // helloWorl console.log(str.slice()); // helloWorld console.log(str.slice(-3,-1)); // rl
-
match() 检查一个字符串是否匹配一个正则表达式
var str='IdnikafhVU' var reg=/[A-Z]/g console.log(str.match(reg)); //IVU
-
test() 方法执行一个检索,用来查看正则表达式与指定的字符串是否匹配。返回
true
或false
。var str='123' var reg=/[1-9]/ console.log(reg.test(str));
-
replace() 用来查找匹配的正则表达式的字符串,然后使用新的字符串代替匹配的子字符串
- 可以是字符或者正则表达式
var str= 'hello java' console.log(str.replace('java','Vue')); console.log(str.replace(/a/g,'A'));
-
search() 执行一个正则表达式匹配查找。如果查找成功,返回索引值 。否则返回-1
var str='AbsssssFfEkk' var reg=/[A-Z]/g console.log(str.search(reg)); //返回值为0
-
split() 把字符串划分为字串,字串做成一个字符串数组
- str.split([separator[, limit]])
- separator可以是一个字符也可以是正则表达式
- limit一个整数 返回分割的数组长度 当在0-length之间会返回指定的数组长度,大于或小于都返回所有内容
var str='hello world vue and react' console.log(str.split(' ')); console.log(str.split(' ',1)); //[hello] console.log(str.split(' ',-1)); console.log(str.split(' ',200)); //[hello,world,vue,and,react]
-
length 字符串的长度
-
toLowerCase() 将整个字符串转为小写
-
toUpperCase() 将整个字符串转为大写
-
trim() 删除字符串两端的空白符
var str=' hello world' console.log(str.trim()); //hello world