字符串的长度
使用length属性可以查看字符串的长度
转化为字符串
toString()
使用对象,数值,布尔值和字符串值都有该方法。但是null,undefined方法没有
let test1 = [1,2,4];
console.log(test1.toString()); // 1,2,4
let test2 = 3.3;
console.log(test2.toString()); // 3.3
let test3 = false;
console.log(test3.toString()); // false
let test4 = 'aaa';
console.log(test4.toString()); // aaa
let test5 = null;
console.log(test5.toString()); //error
let test6 = undefined;
console.log(test6.toString()); // error
String()
String()方法可以将任何类型转换为字符串,包括null,undefined。
let test1 = [1,2,4];
console.log(String(test1)); // 1,2,4
let test2 = 3.3;
console.log(String(test2)); // 3.3
let test3 = false;
console.log(String(test3)); // false
let test4 = 'aaa';
console.log(String(test4)); // aaa
let test5 = null;
console.log(String(test5)); //null
let test6 = undefined;
console.log(String(test6)); // undefined
查找字符方法
charAt()
查找某个位置的字符。
let stringValue = "hello world";
console.log(stringValue.charAt(1)); // e
console.log(stringValue.charAt(20)); // 没有,为空
console.log(stringValue.charAt(-1)); // 一样是空
charCodeAt()
查找某个位置的字符编码。
let stringValue = "hello world";
console.log(stringValue.charCodeAt(1)); // 101
console.log(stringValue.charCodeAt(-1)); // NaN
console.log(stringValue.charCodeAt(20)); // NaN
使用数字索引访问
let stringValue = "hello world";
console.log(stringValue[1]); // e
console.log(stringValue[20]); // undefined
console.log(stringValue[-1]); // undefined
操作字符串
concat()
用于拼接字符串,返回新的字符串。不改变原字符串。
let stringValue = "hello ";
let result = stringValue.concat("world");
console.log(result); //hello world
console.log(stringValue); //hello
也可以直接使用“+”符号来操作字符串。
slice()和substr()和subString()
这三个字符串都是基于子字符串创建新字符串的方法。
都包含两个参数:
- 第一个表示子字符串的位置;
- 第二个参数可选。slice()函数和subString()函数的第二个参数表示子字符串最后一个字符后面的位置。substr()字符串表示返回的字符个数。
let stringValue = "hello world";
console.log(stringValue.slice(3)); // lo world
console.log(stringValue.slice(3,7)); // lo w
console.log(stringValue.substr(3)); // lo world
console.log(stringValue.substr(3,7)); // lo worl
console.log(stringValue.substring(3)); // lo world
console.log(stringValue.substring(3,7)); // lo w
在参数为负值的情况下,参数有很大的不同。
- slice()方法将传入的负值与字符串长度相加
- substr()方法将负的第一个参数加上字符串长度,负的第二个参数转换为0
- substring()方法会把所有负值都转换为0
let stringValue = "hello world";
console.log(stringValue.slice(-3)); // rld
console.log(stringValue.slice(-3,-4)); // 空
console.log(stringValue.substr(-3)); // rld
console.log(stringValue.substr(-3,-4)); // 空
console.log(stringValue.substring(-3)); // hello world
console.log(stringValue.substring(-3,-4)); // 空
字符串位置方法
indexOf()
从前向后查找字符所在的位置。第二个参数可选,表示从指定位置向后查找。
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.indexOf("o", 6)); // 7
lastIndexOf()
从后向前查找字符所在位置。第二个参数可选,表示从指定位置向前查找。
let stringValue = "hello world";
console.log(stringValue.lastIndexOf("o")); // 7
console.log(stringValue.lastIndexOf("o", 6)); // 4
trim()
删除字符串前后空格。并且不影响原字符串。
let stringValue = " hello world ";
let trimStringValue = stringValue.trim();
console.log(stringValue); // hello world
console.log(trimStringValue); //hello world
字符串大小写转换
toLowerCase()
将字符串所有字符转换为小写字符。
let stringValue = "Hello World";
console.log(stringValue.toLowerCase()); // hello world
toLocaleLowerCase()
针对特定地区将字符串所有字符转换为小写字符。
let stringValue = "Hello World";
console.log(stringValue.toLocaleLowerCase());// hello world
toUpperCase()
将字符串所有字符转换为大写字符。
let stringValue = "Hello World";
console.log(stringValue.toUpperCase());// HELLO WORLD
toLocaleUpperCase()
针对特定地区将字符串所有字符转换为大写字符。
let stringValue = "Hello World";
console.log(stringValue.toLocaleUpperCase());// HELLO WORLD
模式匹配方法
match()
匹配符合条件的字符串。只接受一个参数,正则表达式或者RegExp对象。匹配结果为一个数组。
let text = "cat, bat, sat, fat";
let pattern = /.at/;
let matches = text.match(pattern);
console.log(matches); // ["cat", index: 0, input: "cat, bat, sat, fat"]
console.log(matches.index); // 0
console.log(matches[0]); // cat
console.log(pattern.lastIndex); // 0
search()
返回字符串中第一个匹配项的索引。若没有,返回-1。从前向后查找字符串。其接受的参数与match相同。
let text = "cat, bat, sat, fat";
let pattern = /.at/;
console.log(text.search(pattern)); // 0
replace()
替换匹配到的相应字符串。第一个参数可以是正则表达式,也可以是字符串,第二个参数可以是字符串或函数。一般情况下只会替换匹配的第一个字符串,若要全局替换,需要使用正则表达式的全局标志。
let text = "cat, bat, sat, fat";
console.log(text.replace("at", "ond"));// cond, bat, sat, fat
console.log(text.replace(/at/g, "ond")); // cond, bond, sond, fond
split()
基于指定分隔符将字符串拆分为数组。第一个参数表示指定的分隔符,可以是字符串或正则表达式;第二个参数可选,表示数组长度。
let colorText = "red,blue,green,yellow";
console.log(colorText.split(','));// ["red", "blue", "green", "yellow"]
console.log(colorText.split(',',2));// ["red", "blue"]
console.log(colorText.split(/,/));// ["red", "blue", "green", "yellow"]
字符串比较方法
localeCompare()
比较两个字符串,根据返回值确定大小。
let stringValue = "yellow";
console.log(stringValue.localeCompare("brick")); // 1
console.log(stringValue.localeCompare("yellow")); // 0
console.log(stringValue.localeCompare("zoo")); // -1
字符编码转字符
fromCharCode()
接受一个或多个字符编码,将它们转换成一个字符串。
console.log(String.fromCharCode(104,101,108,108,111));// hello