JavaScript-String对象 (读书笔记)

2017-03-09  本文已影响32人  coolheadedY

学习前先放上第一篇写的文章,这篇再次学习数组我们来讲一下上次没讲到的JS数组、字符串、数学函数
小总结
length-长度属性
charAt(idx)-返回制定位置字符串
charCodeAt(idx)-返回unicode码
concat()-连接字符串
slice()-提取字符串
substring()-提取字符串,会转化负数为0或调换参数位置
substr(idx, num)- 提取字符串,第二个参数为个数
indexOf(), lastIndexOf() - 确定字符串的位置
trim() - 去空符号
toLowerCase(), toUpperCase() -转化大小写
localeCompare() - 比较字符串
match() - 匹配字符串,返回数组并带有index、input属性 - 接受正则
search() - 与match类似 - 接受正则
replace() - 替换字符串 - 接受正则
split() - 按规则分割为数组

String对象的属性和方法

length属性

length属性返回字符串的长度

'hello'.length //5

charAt()

charAt方法返回指定位置的字符串

var str = 'hello'
str.charAt(1) //'e'
str.charAt(str.length - 1) //'o'

可以用数组下标替代

'hello'.charAt(1) //'e'
'hello'[1] //'e'

如果参数为负数,或大于等于字符串的长度,charAt返回空字符串

'abc'.charAt(-1) // ""
'abc'.charAt(3) // ""

charCodeAt()

charCodeAt方法返回给定位置字符的Unicode码点(十进制表示),相当于String.fromCharCode()的逆操作。

'b'.charCodeAt() // 98
String.fromCharCode(98) // b

如果不给下标直接返回当前字符串的第一位,如果给下标返回字符串对应位置字符的Unicode编码

'abc'.charCodeAt() // 97
'abc'.charCodeAt(1) // 98

charCodeAt方法返回的Unicode码不得大于65536(0xFFFF)

concat()

concat方法用于连接两个字符换,不改变原字符串

var str1 = 'a'
var str2 = 'b'
str1.concat(str2) // 'ab'

该方法可以接受多个参数

'a'.concat('b','c') // 'abc'

如果参数不是字符串,cancat方法会把参数转化为字符串再连接

'a'.concat(1,2) //'a12' 这里数字1 2是number对象

slice()

slice方法用于提取字符串,不改变原字符串,slice(开始位置,结束位置)

'hello world'.slice(0, 2) // 'he'

如果只传一个参数,则返回此参数位置后的所有字符

'hello world'.slice(2) // "llo world"

如果参数是负数,负值表示从倒数位置开始

'hello world'.slice(0, -5) // "hello "
'hello world'.slice(0, -7) // "hell"
'hello world'.slice(2, -7) // "ll"

如果第一个参数(开始位置)大于第二个参数(结束位置) 返回空字符

'hello world'.slice(4,0) // ""

substring()

substring方法与slice相似,但是有些区别:
如果第二个参数大于第一个参数,substring方法会自动更换两个参数的位置

'hello world'.substring(0, 4) // "hell"
'hello world'.substring(4, 0) // "hell"

如果参数为负数,则转化为0

'hello world'.substring(-3) // "hello world"
'hello world'.substring(3,-3) //"hel"  这里参数可以看做(3,0) ,然后substring方法更换了参数位置为(0,3)

substr()

substr方法也用于取出字符串,不过第一个参数表示开始位置,第二个参数表示取出的长度

'hello world'.substr(3, 3) // "lo "

如果只有一个参数,表示直接取到字符串结尾

'hello world'.substr(3) // "lo world"

如果第一个参数是负数,表示倒数的字符位置。如果第二个参数是负数,则转为0,取0个,返回空字符

'hello world'.substr(-3)  // "rld"
'hello world'.substr(3,-3) // ''

indexOf(), lastIndexOf()

这个两个方法用于确定一个字符串再另一个字符串中的文职,都返回一个整数,表示匹配开始的位置。不匹配则返回0,indexOf从头匹配,lastIndexOf从尾部匹配

'hello world'.indexOf('o') // 4
'hello world'.indexOf('wor') // 6
'hello world'.lastIndexOf('o') // 7

这两个方法还接受第二个参数(整数),表示从该位置开始匹配第一个参数的字符串

'hello world'.indexOf('o', 6) // 7
'hello world'.lastIndexOf('o', 6) // 4

trim()

trim方法表示取出字符串首尾的空格,返回新字符串

'  hello world  '.trim() // 'hello world'

该方法去除的不仅是空格,还包括制表符(\t、\v)、换行符(\n)和回车符(\r)。

'\r\nabc \t'.trim() // 'abc'

toLowerCase(), toUpperCase()

toLowerCaser转换为小写,toUpperCaser转换为大写,返回新字符串

'Hello World'.toLowerCase()// "hello world"
'Hello World'.toUpperCase()// "HELLO WORLD"

这个方法也可以将布尔值和数组转换为大小写,但是要用call使用

String.prototype.toUpperCase.call(true) //'TRUE'
String.prototype.toLowCase.call(['A', 'B']) //'a, b' 
//都被转换为字符串

localeCompare()

localeCompare方法用于比较两个字符串。str1 < str2 返回 -1,str1 > str2 返回1, str1 == str2 返回0, 比较是根据字符的unicode码来比较的,字母的大小写unicode码是不一样的

'a'.localeCompare('ba') // -1
'a'.localeCompare('a') // 0
'b'.localeCompare('a') // 1

match()

match方法用于确定原字符串是否匹配某个字符,返回数组,返回的值为匹配的第一个字符串,如果无匹配到则返回null

'ca, ba, cda'.match('a') // ['a']
'cat, bat, sat, fat'.match('at') // ["at"]
'ca, ba, cda'.match('e') // null

返回的数组具有index属性和input属性,分别表示匹配字符串开始的位置和原始字符串

var match = 'ak, ek, akb, ake'.match('ak')
match.index // 0 匹配的位置是从0开始的
match.input  //  'ak, ek, akb, ake' 原始字符串

search()

search方法与match相似,返回的是匹配的第一个位置,如果没有匹配到返回-1,此方法类似搜索与否

'cat, bat, sat, fat'.search('at') //1
'cat, bat, sat, fat'.search('ba') //5
'cat, bat, sat, fat'.search('d') //-1

replace()

replace方法用于替换匹配的字符串,一般只替换第一个匹配,配合正则的全局g可以替换所有字符

'aaa'.replace('a', 'b') // "baa"
'aaa'.replace(/a/g, 'b') // "bbb"

split()

split方法安装给定的规则分割字符串为数组

'a|b|c'.split('|') // ["a", "b", "c"]
'a|b|c'.split('') // ["a", "|", "b", "|", "c"]

如果不加参数则返回一个具有原字符串的数组

'a|b|c'.split() // ["a|b|c"]

如果满足分割规则的两个部分中间没其他字符,则返回数组之中会出现空字符串

'a||c'.split('|') // ['a', '', 'c']

如果满足分割规则的部分处于字符串的开头或结尾,则返回数组的第一个或最后一个位置的空字符串

'|b|c'.split('|') // ["", "b", "c"]
'a|b|'.split('|') // ["a", "b", ""]

此方法接受第二个参数,限定返回数组的最大长度

'a|b|c'.split('|', 0) // []
'a|b|c'.split('|', 1) // ["a"]
'a|b|c'.split('|', 2) // ["a", "b"]
'a|b|c'.split('|', 3) // ["a", "b", "c"]
'a|b|c'.split('|', 4) // ["a", "b", "c"]

参考

上一篇下一篇

猜你喜欢

热点阅读