《JavaScript高级程序设计》 -- String类型

ECMAScript常用的原始类型有String类型,我们可以通过字面量直接声明一个字符串

var str = 'hello world!'
str.length //12
str.charAt(2) //l

原始类型的数据本身不是对象所以不应该有属性和方法才对但是String类型的原始值却可以引用一些方法

可以发现声明的字符串变量str可以正常的调用一些方法,这两个方法都是以读的方式访问的,一般以读的方式访问字符串时后台都会自动执行以下三步:

  • 创建一个String类型的实例

  • 调用实例上的特定方法

  • 销毁实例

    str.charAt(2) 运行时其实是执行了如下的行为:

    var str = new String('hello world!');
    str.indexOf(2); //l
    str = null;
    

这种行为可以让原始值拥有对象的行为。对于布尔值和数值而言也有相同的包装类型(Boolean和Number)
String对象的所有方法都可以在字符串原始值上面调用。

String对象的常用方法

字符查找

charAt()

返回指定索引位置的字符

var str = new String('hello world!');
//charAt()不传递参数默认索引值为0即返回第一位的字符值
str.charAt()//"h"
//返回指定索引值为3的字符
str.charAt(3)//"l"

//不在索引范围内返回空字符
str.charAt(-6)//""
str.charAt(15)//""

charCodeAt()

返回指定索引位置字符编码

var str = new String('hello world!');

//charCodeAt()不传递参数默认为0索引值即返回第一位的字符编码
str.charCodeAt()//104
//返回指定索引值为3的字符编码
str.charCodeAt(3)//108

//不在索引范围内返回NaN
str.charCodeAt(-6)//NaN
str.charCodeAt(15)//NaN

fromCharCode()

根据指定的码元创建字符串中的字符

var str = String.fromCharCode(104,108,97)//"hla"

字符串操作方法

concat()

将一个或者多个字符串拼接成一个新的字符串

var str1 = 'hello ';
//concat可以传递多个参数,即一次可以拼接多个字符串
var str2 = str1.concat('world','!'); //"hello world!"

常用拼接字符串还是使用加号操作符(+)更方便

slice()

传递两个参数:提取字符串的开始位置和可选的提取字符串的结束位置

var str = 'hello world!'
str.length //12
//不传递参数默认提取整个字符串
str.slice() //"hello world!"
//一个参数时,默认为开始提取位置,结束位置为字符串长度(提取到字符串末尾)
str.slice(6)//"world!"
//两个参数,提取索引值大于等于0小于6之间的字符串)
str.slice(0,6)//"hello "
//提取超出的范围自动省略
str.slice(6,15)//"world!"
//如果参数值为负数把所有值转换为字符串长度加上负值,提取索引值大于等于0小于4之间的字符串
str.slice(0,-8) "hell"
//提取索引值大于等于6小于8之间的字符串
str.slice(-6,-4)//"wo"
//索引值反向(8,6) 返回为空
str.slice(-4,-6)//""


substring()

传递两个参数:提取字符串的开始位置和可选的提取字符串的结束位置

substring()和slice的区别:
参数为负数时subString()方法会把所有负参数值都转换为0
参数值反向时,自动转换为数值小的作为开始位置

var str = 'hello world!'
str.length //12
//不传递参数默认提取整个字符串
str.substring() //"hello world!"
//一个参数时,默认为开始提取位置,结束位置为字符串长度(提取到字符串末尾)
str.substring(6)//"world!"
//两个参数,提取索引值大于等于0小于6之间的字符串)
str.substring(0,6)//"hello "
//提取超出的范围自动省略
str.substring(6,15)//"world!"
//所有负参数值都转换为0,提取索引值大于等于0小于0之间的字符串
str.substring(-6,-4)//""
//提取索引值大于等于0小于8之间的字符串
str.substring(-6,8)//"hello wo"
//参数值反向时,自动转换,数值小的作为开始位置
//提取索引值大于等于0小于2之间的字符串
str.substring(2,-8)//"he"
//提取索引值大于等于1小于4之间的字符串
str.substring(4,1)//"ell"

substr()

传递两个参数:提取的开始位置和可选的提取子字符串的个数

substr()把第一个负参数值当成字符串长度加上负值,把第二个负参数值转换为0

var str = 'hello world!'
str.length //12
//不传递参数默认提取整个字符串
str.substr() //"hello world!"
//一个参数时,默认为开始提取位置,结束位置为字符串长度(提取到字符串末尾)
str.substr(6) //"world!"
//两个参数,从索引值大于等于0开始向后面6个子字符串)
str.substr(0,6) //"hello "
//提取超出的范围自动省略
str.substr(6,15) //"world!"
//提取从索引值6开始,后面0个字符(只要第二个参数值为负数,则最终的结果为空字符串)
str.substr(-6,-4) //""
//提取索引值6开始,后面3个字符
str.substr(-6,3) //"wor"

字符串位置方法

indexOf()和lastIndexOf()返回子字符串的位置(未找到返回-1)

传递两个参数:搜索的子字符串和可选的搜索的起始位置

indexOf()

从字符串开头向后查找子字符串

var str = 'hello world!';
//传递一个参数默认从字符串开始位置开始查找
str.indexOf('llo') //2
//传递两个参数,指定开始搜索的索引位置
str.indexOf('l',1) //2
str.indexOf('l',4)//9
//当第二个参数查找位置为负数是默认从开始位置开始查找
str.indexOf('o',-2)//4
//没找到返回-1
str.indexOf('ee')//-1

lastIndexOf()

从字符串末尾向前查找子字符串

var str = 'hello world!';
//传递一个参数默认从字符串末尾开始向前查找
str.lastIndexOf('o')
//传递两个参数,指定索引位置向前查找
str.lastIndexOf('o',8)//7
str.lastIndexOf('o',6)//4
//负数返回-1
str.lastIndexOf('o',-1)//-1

字符串包含方法

用于判断字符串中是否包含另外一个字符串的方法,返回一个布尔值

startsWith()

检查起始位置索引开始的匹配项

传递两个参数:判断的子字符串和可选的字符串中开始匹配的起始位置

var str = 'hello world!';
//str字符串是否以'hello'字符串开头
str.startsWith('hello')//true
//str字符串是否以'llo'字符串开头
str.startsWith('llo')//false
//通过第二个参数修改开始匹配的位置
str.startsWith('llo',2)//true

endsWith()

检查索引为末尾开始的匹配项

传递两个参数:判断的子字符串和可选的当做字符串末尾的位置

var str = 'hello world!';
//str字符串是否以'd!'字符串结尾
str.endsWith('d!')//true
//str字符串是否以'rld'字符串结尾
str.endsWith('rld');//false
//通过第二个参数修改字符串结尾的位置
str.endsWith('rld',11);//true

includes()

检查整个字符串

传递两个参数:判断的子字符串和可选的字符串中开始匹配的起始位置

var str = 'hello world!';
//str字符串中是否包含'llo',从字符串起始位置开始查找
str.includes('llo') //true
//传递两个参数修改查找的位置
str.includes('llo',3)//false
//在整个字符串中查找没有找到返回false
str.includes('eee')//false

trim()方法

清除字符串前后的空格(不影响原始值)

var str = ' hello ';
str.length//7
var str1 = str.trim();//'hello'
console.log(str1.length) //5


//trimLeft和trimRight方法分别用来清除字符串开始和末尾的空格
console.log(str.trimLeft())//'hello '
console.log(str.trimRight())//' hello'

repeat()方法

传递一个整数参数,表示将字符串复制多少次

var str = 'hh ';
str.repeat(5)//"hh hh hh hh hh "

padStart()和padEnd()方法

padStart()在字符串前面填充指定字符或字符串

传递两个参数:长度和可选的填充字符或字符串(默认填充为空)

var str = 'foo'
//指定的长度小于或者等于字符串长度,则返回原始字符串
console.log(str.padStart(2)) //"foo"

//不传递第二个参数,默认在前面填充空格
console.log(str.padStart(6)) //"   foo"

//指定长度为6,不够默认前面填充为.
console.log(str.padStart(6,'.')) //"...foo"

//指定长度为6在前面填充'barr',超出部分自动省略
console.log(str.padStart(6,'barr')) //"barfoo"

padEnd()在字符串后面填充指定字符或字符串

传递参数:长度和可选的填充字符或字符串(默认填充值为空)

var str = 'foo'
//不传递第二个参数,默认在后面填充空格
str.padEnd(6) //"foo   "
//指定长度为6,不够在后面填充为.
str.padEnd(6,'.')//"foo..."
//指定长度为6在字符串后面填充'barr',超出部分自动省略
str.padEnd(6,'barr') //"foobar"

字符串迭代与结构

字符串原型上面暴露了一个@@iterator方法,表示可以迭代字符串的每个字符。

利用for-of可以方便的遍历每一个字符

var str = 'hello';
for(const c of str) {
	console.log(c) // h e l l o
}

有了这个迭代器,字符串就可以通过解构操作符来解构了。

var str = 'hello';
var arr = [...str] //["h", "e", "l", "l", "o"]

字符串大小写转换

字符串转换为大写的方法:toUpperCase()和toLocaleUpperCase()

var str = 'Hello';
console.log(str.toUpperCase())//HELLO
console.log(str.toLocaleUpperCase())//HELLO

字符串转换为小写的方法:toLowerCase()和toLocaleLowerCase()

var str = 'Hello';
console.log(str.toLowerCase())//hello
console.log(str.toLocaleLowerCase()) //hello

toLocaleUpperCase()和toLocaleLowerCase()基于特定地区实现。

localeCompare()方法

比较两个字符串

  • 按照字母表顺序,字符串应该排在字符串参数前头,则返回负值(一般-1)
  • 相等,返回0
  • 按照字母表顺序,字符串应该排在字符串参数后面,则返回正值(一般为1)
var str = 'yellow'
str.localeCompare('blue') //1
str.localeCompare('yello') //0
str.localeCompare('zoo') //-1

Guess you like

Origin blog.csdn.net/weixin_43398820/article/details/117904205