数据类型-String
- String 是用来表示和操作字符串的对象
- String 全局对象是一个用于字符串或一个字符序列的构造函数。
- 所有用
""
(双引号),''
(单引号),``
(反引号:ES6模板字符串)包起来的都是字符串 - 每一个字符串都是由零到多个字符组成的
长字符串
- 可以使用 + 运算符将多个字符串连接起来
let longString = "This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";
- 可以在每行末尾使用反斜杠字符(“\”),以指示字符串将在下一行继续。
// 确保反斜杠后面没有空格或任何除换行符之外的字符或缩进; 否则反斜杠将不会工作。
let longString = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
把其他类型值转化为字符串
- String(thing)
- new String(thing)
- thing : 任何可以被转换成字符串的值。
- [val].toString()
- null 和undefined 禁止使用toString方法,(null).toString()->报错,但是转换结果和undefined一样,就是"null"/“undefined”
- 普通对象.toString()的结果是"[object, object]" => Object.prototype.toString方法不是转换为字符串的,而是用来检测数据类型的
- ([]).toString() ->""
- 字符串拼接
- 四则运算法则中,除加法以外,其余的都是数学计算,只有“+”可能存在字符串拼接(加号两侧一旦出现字符串或者对象,则不是数学运算,而是变成字符串拼接)=>10+ “10” //“1010” ; 10+{} //=>“10[object object]”
练习题
let a = 1 + null + true + []+ null + undefined +[] +{} +false
console.log(a) //=>"2nullundefined[object Object]false"
/*解析:
1 + null => 1 + 0 => 1
1 + true => 1 + 1 => 2
2 + [] => 2 + "" => "2"
"2" + null => "2null"
"2null" + undefined => "2nullundefined"
"2nullundefined" + [] => "2nullundefined" + "" => "2nullundefined"
"2nullundefined" + {} => "2nullundefined" + "[object object]" =>"2nullundefined[object, object]"
"2nullundefined[object, object]" + false =>"2nullundefined[object object]false"
*/
字符串中的常用方法
- charAt / charCodeAt
/*
charAt:根据索引获取指定位置的字符
charCodeAt:根据索引获取指定字符的ASII码值(Unicode编码值)
@params
n [number]获取字符指定的索引
@return
返回查找到的字符(找不到反悔的是空字符串,不是undefined)或者对应的编码值
*/
let str = "helloworldhappyeveryday";
console.log(str.charAt(0)); //=>"h"
console.log(str[0]); //=>"h"
console.log(str.charAt(10000)); //=>""
console.log(str[10000]); //=>undefined
console.log(charCodeAt(0)); //=>104
console.log(String.fromCharCode(104)); //=>"h"
- substr / substring / slice
/*
都是为了实现字符串截取(在原字符串中找到自己想要的)
substr(n,m):从索引n开始截取m个字符,m不写截取到末尾(后面的的方法也是)
substring(n,m):从索引n开始找到索引为m处,m不写截取到末尾(不含m)
slice(n,m):和substring一样,都是找到索引为m处,但是slice可以支持负数作为索引,其余两个方法是不可以的。
*/
let str = "helloworldhappyeveryday";
console.log(str.substr(2,8));//=>"lloworld"
console.log(str.substr(2));//=>"lloworldhappyeveryday"
console.log(str.substr(2,100));//=>"lloworldhappyeveryday"
console.log(str.substr(-2,10));//"ay"
console.log(str.substring(2,8));//=>"llowor"
console.log(str.substring(2));//=>"lloworldhappyeveryday"
console.log(str.substring(2,100));//=>"lloworldhappyeveryday"
console.log(str.substring(-2,10));//"helloworld" substring不支持负数索引
console.log(str.slice(2,8));//=>"llowor"
console.log(str.slice(2));//=>"lloworldhappyeverydays"
console.log(str.slice(2,100));//=>"lloworldhappyeverydays"
console.log(str.slice(-9,-2));//=>"yeveryd"
//substr 和 slice 支出负数索引,快捷查找负数索引:str.length+(负数索引)
//substr 的第二个参数(个数)不可以为负数。
//截取的个数和索引超字符串总个数的也是截取到末尾
- indexOf / lastIndexof / includes
/*
验证字符是否存在
indexOf(x, y):获取x第一次出现位置的索引,y是控制查找的起始位置的索引
lastIndexOf(x) :获取x最后一次出现位置的索引
=> 没有这个字符,返回结果为-1
字符串的indexOf ,lastIndexOf兼容所有浏览器
数组中的indexOf ,lastIndexOf不兼容ie6-8
*/
let str = "helloworldhappyeveryday";
console.log(str.indexOf("l")); //=>2
console.log(str.lastIndexOf("l")); //=>8
console.log(str.indexOf("l",5)); //=>8
console.log(str.indexOf("happy")); //=>10
console.log(str.indexOf("c")); //=>-1
console.log(str.includes("l")); //=>true
console.log(str.includes("c")); //=>false
//if (str.indexOf("l") ===-1){不包含执行的代码}
//if (!str.includes("l")){不包含执行的代码}
- toUpperCase / toLowerCase / toLocaleUpperCase / toLocaleLowerCase
/*
字符串中字母的大小写
toUpperCase/toLocaleUpperCase() :字符串中字母全部转大写
toLowerCase/toLocaleLowerCase() :字符串中字母全部转小写
toLocaleLowerCase()/toLocaleUpperCase()方法按照本地方式把字符串转换为小写。
只有几种语言(如土耳其语)具有地方特有的大小写映射,
所有该方法的返回值通常与toUpperCase()/toLowerCase()一样。
*/
let str = "helloworldhappyeveryday";
str.toUpperCase();//=>"HELLOWORLDHAPPYEVERYDAY"
str.toLowerCase();//=>"helloworldhappyeveryday"
str.substr(0,1).toUpperCase()+str.substr(1);//=>"Helloworldhappyeveryday" 首字母大写
- split
/*
split([分隔符]):指定一个分隔符,把字符串按照指定的分隔符拆分成数组(和数组中的join相对应)
split 支持正则表达式
*/
let str ="1|2|3|4";
str = str.split("|"); //["1", "2", "3", "4"]
str = str.join(","); //"1,2,3,4"
- replace
/*
replace(老字符,新字符):实现字符串替换(总是伴随着正则表达式使用)
*/
let str = "hello&world&happy&everyday";
str.replace("&","-");//=>"hello-world&happy&everyday"
//=> 在不使用正则表达式的情况下,执行一次REPLACE只能替换一次字符。
str.replace(/&/g,"-"); //=>"hello-world-happy-everyday"
- concat
/*
用于连接两个或多个字符串。
语法:stringObject.concat(stringX,stringX,...,stringX)
str.concat() 与 ary.concat() 很相似,
使用"+"运算符来进行字符串的连接运算通常会更简便一些。
*/
var str1="Hello "
var str2="world!"
str1.concat(str2);//=>"Hello world!"
- trim
/*trim:用于删除字符串的头尾空格,不会改变原始字符串。*/
let str= " 12233 "
str.trim();//=>"12233"
//还有trimRight()