🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# String 字符串对象 >[success] **String对象** 用于处理已有的字符串,字符串可以使用单引号或者双引号。 ## length属性   length 属性包含一个整数,用来指出 String 对象中的字符数。String 对象中的最后一个字符的索引为 length - 1。 ~~~ <script> var str = 'hello world'; console.log('str字符串长度为:'+str.length); // str字符串长度为: 11 </script> ~~~ ## indexOf方法 在字符串中查找字符串   返回 String 对象内第一次出现子字符串的字符位置。 ~~~ <script> var str = 'hello world'; console.log(str.indexOf('world')); //执行结果为 6 </script> ~~~ ## match方法 内容匹配   使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回。 ~~~ <script> var str = 'hello world'; console.log(str.match('world')); // ["world", index: 6, input: "hello world"] </script> ~~~ ## replace方法 替换内容   返回根据正则表达式进行文字替换后的字符串的复制。 ~~~ <script> var str = 'hello world'; console.log(str.replace('world','rose')); // hello rose </script> ~~~ ## toUpperCase方法 toLowerCase方法 字符串大小写转换 ~~~ <script> var str = 'hello world'; console.log(str.toUpperCase()); // HELLO WORLD console.log(str.toLowerCase()); // hello world </script> ~~~ ## split方法 字符串转成数组   将一个字符串分割为子字符串,然后将结果作为字符串数组返回。 ~~~ <script> var str = 'hello world'; console.log(str.split(' ')); // ["hello", "world"] </script> ~~~ ## search方法 字符串查找   返回与正则表达式查找内容匹配的第一个子字符串的位置。 ~~~ <script> var s = "The rain in Spain falls mainly in the plain."; re = /falls/i; // 创建正则表达式模式。 r = s.search(re); // 查找字符串。 console.log(r); // 18 </script> ~~~ ## slice方法 字符串截取   返回字符串的片段。 ~~~ <script> var str = 'hello world'; console.log(str.slice(6,8)); // wo </script> ~~~