String类型

length属性

表示字符串中包含多少个字符。

1 let str='你好123你好';
2 str.length; // 7

charAt()方法

接收一个参数,基于0的字符串位置。

以单字符字符串的形式返回给定位置的那个字符。

1 str.charAt(6); // "好"

charCodeAt()方法

返回字符编码

1 str.charCodeAt(6); // 22909

concat()方法

将一或多个字符串拼接起来,返回拼接得到的信字符串。

1 let resultStr = str.concat('哈哈哈hello');
2 resultStr    // "你好123你好哈哈哈hello"

slice()方法 substring()方法

返回被操作字符串的子字符串。接受一个或两个参数,第一个参数指定子字符串开始的位置,第二个参数表示子字符串到哪里结束(最后一个字符后面的位置)。

1 str = 'hello world';
2 str.slice(3); // "lo world"
3 str.substring(3); // "lo world"
4 str.slice(3,7); // "lo w"
5 str.substring(3,7); //"lo w"

substr()方法

返回被操作字符串的子字符串。接受一个或两个参数,第一个参数指定子字符串开始的位置,第二个参数表示子字符串到哪里结束(返回字符串的个数)。

1 str.substr(3); // "lo world"
2 str.substr(3,7); // "lo worl"

slice()方法 substring()方法  substr()方法当传入的参数是负值时

slice()会将传入的负值与字符串的长度相加

substring()会把所有的负值参数转为0

substr()负的第一个参数加上字符串的长度,负的第二个参数转为0

 1 str.slice(-3); // "rld"
 2 str.slice(-3,-9); // ""
 3 str.slice(-3,-1); // "rl"
 4 str.slice(3,-1); // "lo worl"
 5 
 6 str.substring(-9); // "hello world"
 7 str.substring(-9,-3); // ""
 8 str.substring(-3,-9); // ""
 9 
10 str.substr(-3); // "rld"
11 str.substr(-3,9); // "rld"
12 str.substr(-3,-8); // ""

indexOf()方法

从字符串的开头向后搜索字符串,接受一个或两个参数,返回第一个参数首次出现的位置,第二个参数表示从字符串从哪个位置开始搜索,没有找到返回-1。

1 str.indexOf('l'); // 2
2 str.indexOf('l',3); // 3

lastIndexOf()方法

从字符串的末尾向前搜索字符串,接受一个或两个参数,返回第一个参数首次出现的位置,第二个参数表示从字符串从哪个位置开始搜索,没有找到返回-1。

1 str.lastIndexOf('l'); // 9
2 str.lastIndexOf('l',3); // 3

 trim()方法

会创建一个字符串的副本,删除前置及后缀的所有副本。

1 let str = '     hello   world      ';
2 let str2 = str.trim();
3 str  // "     hello   world      "
4 str2  // "hello   world"

toUpperCase()方法   toLowerCase()方法

toUpperCase()字符串大写转换

toLowerCase()字符串小写转换

1 let str = '     hello   world      ';
2 str.toUpperCase(); // "     HELLO   WORLD      "
3 str // "     hello   world      "
4 let upstr='HSDHSD';
5 upstr.toLowerCase(); // "hsdhsd"

toLocaleUpperCase()方法  toLocaleLowerCase()方法

针对不同地区的大小写转换(推荐使用)

1 str.toLocaleUpperCase();  //"     HELLO   WORLD      "
2 upstr.toLocaleLowerCase();  // "hsdhsd"

match()方法

只接受一个参数,一个正则表达式或一个RegExp对象

1 let pattern = /.at/;
2 let matchs = t.match(pattern); // ["cat", index: 0, input: "cat, bat, sat, fat"]0: "cat"index: 0input: "cat, bat, sat, fat"length: 1__proto__: Array(0)
3 matchs[0] // "cat"
4 pattern = /.at/g;
5 matchs=t.match(pattern);  // (4) ["cat", "bat", "sat", "fat"]
6 matchs[2]; // "sat"
7 pattern.lastIndex //0

search()方法

参数与match()方法相同,返回字符串中第一个匹配项的索引,没有找到返回-1

1 pattern =/at/g;
2 t.search(pattern);  // 1

replace()方法

接受两个参数,第一个参数可以是一个RegExp对象或一个字符串(这个字符串不会被转成正则表达式),第二个参数可以是一个字符串或者一个函数。第一个参数如果是一个字符串则只会替换第一个子字符串,要想替换所有子字符串,唯一的办法就是提供一个正则表达式,指定全局的g标志。

1 t  // "cat, bat, sat, fat"
2 let result = t.replace('at', 'hoho'); // "choho, bat, sat, fat"
3 result = t.replace(/at/,'hoho'); // "choho, bat, sat, fat"
4 result = t.replace(/at/g,'hoho'); // "choho, bhoho, shoho, fhoho"

split()方法

基于指定的分隔符将一个字符串分割成多个子字符串,并将结果放到一个数组中

第二个参数可选,用于指定数组的大小

1 t // "cat, bat, sat, fat"
2 let t1=t.split(','); // (4) ["cat", " bat", " sat", " fat"]0: "cat"1: " bat"2: " sat"3: " fat"length: 4__proto__: Array(0)
3 let t2=t.split(',',2); // (2) ["cat", " bat"]

localeCompare()方法

比较两个字符串:

如果字符串在字母表中应该排在字符串参数之前,返回一个负数(大多数情况是-1)

如果字符串在字母表中等于字符串参数,返回0

如果字符串在字母表中应该排在字符串参数之后,返回一个正数(大多数情况是1)

1 let a='blue';
2 let b='yellow';
3 a.localeCompare(b); // -1
4 b.localeCompare(a); // 1
5 a.localeCompare(a); // 0

fromCharCode()方法

接收一或多个字符编码

1 String.fromCharCode(104,101,108,108,111); // "hello"

 

posted @ 2017-12-16 17:05  biubiu小希希  阅读(224)  评论(0编辑  收藏  举报