1.属性
· String类型的每个实例都有一个length属性,表示字符串包含多个字符。
var stringValue = "hello world"; console.log(stringValue.length); //11
2.方法
· 继承的valueOf()、toLocaleString()、toString()方法,都返回对象所表示的基本字符串值。
· charAt()、charCodeAt():都接收1个参数,即基于0的字符位置。charAt()以单字符字符串的形式返回给定位置的那个字符,charCodeAt()返回的是字符编码。
字符串操作方法: 这4个都 不影响原始值 。var stringValue = "hello world"; console.log(stringValue.charAt(1)); //"e" console.log(stringValue.charCodeAt(1)); //"101" console.log(stringValue[1]); //"e"也可以使用方括号加数字索引来访问字符串中的特定字符
· concat():用于将1或多个字符拼接起来,返回拼接得到的新字符串,可接收任意多个参数。事实上,实践中使用更多的还是加号(+)操作符。
var stringValue = "hello"; var result = stringValue.concat("world","!"); console.log(result); //helloworld!· slice()
>只有1个参数且为正时,返回第一个参数指定的开始位置至最后,当这个参数为负时,将该负值与字符串长度相加,返回相加后指定的开始位置至最后。
>2个参数时,当第2个参数为正时,即返回开始位置至第2个参数指定的结束位置前一位。当第2个参数为负时,将该负值与字符串长度相加,返回开始位置至结束位置前一位。
· substring()
>当参数为1个或2个且都是正的,则和slice()都为正时结果一样,但会把所有负值参数转换为0.而且,这个方法会将较小的数作为开始位置,将较大的数作为结束位置。
· substr()
>和前两者不同的是,第2个参数指定的是返回的个数。而且,当第一个参数为负时,会加上字符串的长度来得出,而第2个参数为负时,转换为0。
var stringValue = "hello world"; console.log(stringValue.slice(3)); //"lo world" console.log(stringValue.substring(3)); //"lo world" console.log(stringValue.substr(3)); //"lo world" console.log(stringValue.slice(3,7)); //"lo w" console.log(stringValue.substring(3,7)); //"lo w" console.log(stringValue.substr(3,7)); //"lo worl" console.log(stringValue.slice(-3)); //"rld" console.log(stringValue.substring(-3)); //"hello world" console.log(stringValue.substr(-3)); //"rld" console.log(stringValue.slice(3,-4)); //"lo w" console.log(stringValue.substring(3,-4)); //"hel" console.log(stringValue.substr(3,-4)); //""空字符串字符串位置方法
· indexOf()、lastIndexOf()——这2个方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到该子字符串,则返回-1。区别在于,indexOf()从字符串的开头向后搜索子字符串,而lastIndexOf()从字符串的末尾向前搜索子字符串。
var stringValue = "hello world"; console.log(stringValue.indexOf("o")); //4 console.log(stringValue.lastIndexOf("o")); //7这2个方法都可以接收可选的第2个参数,表示从字符串中的哪个位置开始搜索。indexOf()会从该参数指定的位置向后搜索,忽略该位置之前的所有字符。而lastIndexOf()会从指定的位置向前搜索,忽略该位置之后的所有字符。
var stringValue = "hello world"; console.log(stringValue.indexOf("o",6)); //7 console.log(stringValue.lastIndexOf("o",6)); //4在使用第2个参数的情况下,可以通过循环调用indexOf()或lastIndexOf()来找到所有匹配的子字符串,如下:
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit "; var positions = new Array(); var pos = stringValue.indexOf("e"); while(pos>-1){ positions.push(pos); pos = stringValue.indexOf("e",pos + 1); } console.log(positions); //(5) [3, 24, 32, 35, 52]· trim() ——这个方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。
var stringValue = " hello world "; var trimmedStringValue = stringValue.trim(); console.log(stringValue); // " hello world " console.log(trimmedStringValue); //"hello world"字符串大小写转换方法
· toLowerCase()、toLocaleLowerCase()、toUpperCase()、toLocaleUpperCase()。一般来说,在不知道自己的代码将在哪种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一点。对有些地区来说,针对地区的方法与其通用方法得到的结果相同,但少数语言,如(土耳其语)会为Unicode大小写转换应用特殊的规则,这时候就必须使用针对地区的方法来保证实现正确的转换。
var stringValue = "hello world"; console.log(stringValue.toLocaleUpperCase()); //"HELLO WORLD" console.log(stringValue.toUpperCase()); //"HELLO WORLD" console.log(stringValue.toLowerCase()); //"hello world" console.log(stringValue.toLocaleLowerCase()); //"hello world"字符串的模式匹配方法
· match()、search()、replace()、split()
· localeCompare()——这个方法比较2个字符串,如果字符串在字母表中应该排在字符串参数之前,则返回一个负数,如果字符串等于字符串参数,则返回0,如果字符串在字母表中应该排在字符串参数之后,则返回一个整数。
var stringValue = "yellow"; console.log(stringValue.localeCompare("brick")); //1 console.log(stringValue.localeCompare("yellow")); //0 console.log(stringValue.localeCompare("zoo")); //-1注意:因为localeCompare()返回的数值取决于实现,所以最好是像下面例子所示的这样使用这个方法。它比较与众不同的地方,就是实现所支持的地区(国家和语言)决定了这个方法的行为。比如,美国以英语作为ES实现的标准语言,因此,localeCompare()就是区分大小写的,于是大写字母在字母表中排在小写字母前头就称为了一项决定性的比较规则。不过,在其他地区恐怕就不是这种情况了。
var stringValue = "yellow"; function determineOrder(value){ var result = stringValue.localeCompare(value); if(result < 0){ console.log("The string 'yellow' comes before the string '" + value +"'." ); }else if(result > 0){ console.log("The string 'yellow' comes after the string '"+ value +"'."); }else{ console.log("The string 'yellow' is equal to the string '"+ value +"'."); } } determineOrder("brick");//The string 'yellow' comes after the string 'brick'. determineOrder("yellow");//The string 'yellow' is equal to the string 'yellow'. determineOrder("zoo");//The string 'yellow' comes before the string 'zoo'.· fromCharCode() ——接收一或多个字符编码,然后将他们转换成一个字符串。从本质上看,这个方法与实例方法charCodeAt()执行的是相反的操作。
console.log(String.fromCharCode(104,101,108,108,111));//"hello"