Javascript类型系统之String字符串类型详解(3)

var str = "hello"; console.log(str[0]);//h console.log(str[[1]]);//e console.log(str[false]);//undefined console.log(str[-1]);//undefined console.log(str[NaN]);//undefined console.log(str[]);//报错

【charCodeAt()】

  charCodeAt()方法类似于charAt()方法,接收一个基于0的字符位置的参数,但返回的是指定位置的字符16位Unicode编码。返回值是一个16位的整数,在0-65535之间,即0x0000-0xffff之间

  参数为空或NaN时,默认参数为0;当参数超出范围时,则返回NaN

var str = "hello"; console.log(str.charCodeAt());//104 console.log(str.charCodeAt(0));//104 console.log(str.charCodeAt(1));//101 console.log(str.charCodeAt(-1));//NaN console.log(str.charCodeAt(10));//NaN console.log(str.charCodeAt(NaN));//104

  同样地,charCodeAt()方法涉及到Number()函数的隐式类型转换,如果转换为数值,则按照上述规则输出相应值;如果转换为NaN,则输出第0个字符的字符编码

var str = "hello"; console.log(str.charCodeAt(true));//101 console.log(str.charCodeAt(false));//104 console.log(str.charCodeAt('abc'));//104 console.log(str.charCodeAt({}));//104 console.log(str.charCodeAt([2]));//l08

【fromCharCode()】

  String构造函数本身有一个静态方法:fromCharCode()。这个方法的任务是接收一个或多个字符编码,然后把它们转换成一个字符串。从本质上看,这个方法与实例方法charCodeAt()执行的是相反的操作。若参数为空为NaN时,则返回空字符串;若参数超出0-65535的范围,则输出字符不可控

console.log(String.fromCharCode(104,101,108,108,111));//'hello' console.log(String.fromCharCode(0x6211,0x662f,0x5c0f,0x706b,0x67f4));//'我是小火柴' console.log(String.fromCharCode());//'' console.log(String.fromCharCode(NaN));//'' console.log(String.fromCharCode(-1)); console.log(String.fromCharCode(65560));

  如果一个字符占用四字节,则需要拆成两个字符表示

console.log(String.fromCharCode(0xD842, 0xDFB7)); // "𠮷"

字符串拼接

  关于字符串拼接共有concat()和加号+两种方法

【concat()】

  concat()方法用于将一个或多个字符串拼接起来,返回拼接得到的新字符串,而原字符串不发生改变。若参数(第一个参数除外)不是字符串,则通过String()方法隐式转换为字符串,再进行字符串拼接

var stringValue = 'hello '; var result = stringValue.concat('world','!'); console.log(result);//'hello world!' console.log(stringValue);//'hello'

  [注意]第一个参数只能是字符串,如果是其他类型(数组除外)则报错

(1).concat('2');//报错
(true).concat('false');//报错
({}).concat('abc');//报错

  [注意]由于数组也存在concat()方法,参数会按照首先出现的参数是数组还是字符串来决定如何转换

'1,2,3,'.concat([4,5]);//'1,2,3,4,5'
[1,2,3].concat(',4,5');//[1, 2, 3, ",4,5"]

【加号运算符(+)】

  虽然concat()是专门用来拼接字符串的方法,但实践中使用更多的还是加号运算符(+)。使用加号运算符在许多时候都比concat()简单易行

var stringValue = 'hello '; console.log(stringValue.concat('world','!'));//'hello world!' console.log(stringValue + 'world' + '!');//'hello world!'

  [注意]当操作数其中一个是字符串,或者对象转换为字符串时,才进行字符串拼接

1 + 2;//3 '1' + 2;//'12' var o = {valueOf:function(){return '1';}}; o + 2;//'12' var o = {valueOf:function(){return 1;}}; o + 2;//3

创建子字符串

  创建子字符串共有slice()、substr()和substring()三种方法

【slice()】

  slice(start,end)方法需要两个参数start和end,返回这个字符串中从start位置的字符到(但不包含)end位置的字符的一个子字符串;如果end为undefined或不存在,则返回从start位置到字符串结尾的所有字符

  如果start是负数,则start = max(length + start,0)

  如果end是负数,则end = max(length + end,0)

  start和end无法交换位置

var stringValue = 'hello world'; console.log(stringValue.slice());//'hello world' console.log(stringValue.slice(2));//'llo world' console.log(stringValue.slice(2,undefined));//'llo world' console.log(stringValue.slice(2,-5));//'llo ' console.log(stringValue.slice(2,-20));//'' console.log(stringValue.slice(20));//'' console.log(stringValue.slice(-2,2));//'' console.log(stringValue.slice(-2,-20));//'' console.log(stringValue.slice(-2,20));//'ld' console.log(stringValue.slice(-20,2));//'he' console.log(stringValue.slice(-20,-2));//'hello wor'

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzsjxw.html