JavaScript之String常用方法

chartAt和charCodeAt和codePointAt

  • charAt:返回指定位置的字符
let s = "hello"
console.log(s.charAt(2)) // l
  • charCodeAt:返回指定位置的字符编码
let s = "hello"
console.log(s.charCodeAt(2)) //108

注:当需要4个字节存储汉字时(𠮷),返回前两个字节和后两个字节的值

let str = '𠮷'
console.log(str.charCodeAt(0)) //55362
console.log(str.charCodeAt(1))//57271
  • codePointAt:能够正确处理4个字节存储的字符,返回一个字符的码点
let str = '𠮷'
console.log(str.codePointAt(0)) //134071

concact

  • concact用于连接两个或多个字符串
let s2 = "hhh"
console.log(s2.concat([10])) // 'hhh10'

concat() 方法将把它的所有参数转换成字符串,然后按顺序连接

includes和startsWith和endsWith

这三个方法都支持第二个参数,表示开始搜索的位置

  • includes:返回布尔值,表示是否找到了参数字符串
  • startsWith:返回布尔值,表示参数字符串是否在源字符串的头部
  • endsWith:返回布尔值,表示参数字符串是否在源字符串的尾部
let s = "hello world"
console.log(s.startsWith("hello")) //true
console.log(s.endsWith("ello world")) //true
console.log(s.includes("llo wo")) //true

console.log(s.startsWith("world",6)) //true
console.log(s.endsWith("hello",5)) //true
console.log(s.includes("hello",6)) //false

indexOf和lastIndexOf

  • indexOf:查找某字符串在字符串首次出现的位置
  • lastIndexOf:返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索
let s = "hello world"
console.log(s.indexOf('e')) //1
console.log(s.indexOf('e',3)) //-1

console.log(s.lastIndexOf('l')) //9 
console.log(s.lastIndexOf('l',8)) //3

match和matchAll

  • match:方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
console.log(s.match('hello')) //["hello", index: 0, input: "hello world", groups: undefined]
console.log(s.match('Hello'))//null
console.log(s.match('helo')) //null
console.log(s.match('world')) //["world", index: 6, input: "hello world", groups: undefined]

var str2="1 plus 2 equal 3"
console.log(str2.match(/\d+/g)) //["1", "2", "3"]
console.log(str2.match(/\d+/)) //["1", index: 0, input: "1 plus 2 equal 3", groups: undefined]
  • matchAll:返回所有与正则表达式匹配字符串的结果的迭代器,包括捕获组。
let regexp = /t(e)(st(\d?))/g;
let str3 = 'test1test2';
let array = [...str3.matchAll(regexp)];
console.log(array[0]); //["test1", "e", "st1", "1", index: 0, input: "test1test2", groups: undefined]
console.log(array[1]);// ["test2", "e", "st2", "2", index: 5, input: "test1test2", groups: undefined]

padStart和padEnd

  • padStart:如果某字符串不够指定长度,会在头部补全
  • padEnd:如果某字符串不够指定长度,会在尾部补全

console.log('x'.padStart(5,'ab')) //ababx
console.log('x'.padEnd(5,'ab')) //xabab
console.log('xx'.padStart(2,'ab')) //xx
console.log('xx'.padEnd(2,'ab'))//xx
console.log('abc'.padStart(10,'0123456789')) //0123456abc
console.log('x'.padStart(4))//'    x'
console.log('x'.padEnd(4))//'x  

repeat

  • 返回一个新字符串,表示将原字符串重复n次
console.log('x'.repeat(3))//'xxx'
console.log('hello'.repeat(2))//'hellohello'
console.log('na'.repeat(0))//''
console.log('na'.repeat(2.9))//'nana'
console.log('na'.repeat(Infinity))//RangeError
console.log('na'.repeat(-0.9))//""
console.log('na'.repeat(NaN))//""
console.log('na'.repeat("na"))//""
console.log('na'.repeat("3"))//"nanana"

replace

  • 在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
var str4="Visit Microsoft!"
console.log(str4.replace(/Microsoft/, "W3School"))

var str5="Welcome to Microsoft! "
str5=str5 + "We are proud to announce that Microsoft has "
str5=str5 + "one of the largest Web Developers sites in the world."
console.log(str5.replace(/Microsoft/g, "W3School"))

var name = "Doe, John";
console.log(name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1"))

name = '"a", "b"';
console.log(name.replace(/"([^"]*)"/g, "'$1'"))//'a', 'b'

name = 'aaa bbb ccc';
uw=name.replace(/\b\w+\b/g, function(word){
return word.substring(0,1).toUpperCase()+word.substring(1);}
		);

search

  • search:检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
    注释:要执行忽略大小写的检索,请追加标志 i。
var str6 = "Hello World"
console.log(str6.search("el"))//1
console.log(str6.search(/hello/))//-1
console.log(str6.search(/hello/i))//0

slice、substring、substr

  • slice:提取字符串的某个部分,并以新的字符串返回被提取的部分。
var str7="Hello happy world!"
console.log(str7.slice(6)) //happy world!
console.log(str7.slice(6,11)) //happy
  • substring:用于提取字符串中介于两个指定下标之间的字符。(不接受负的参数)
console.log(str7.substring(6)) //happy world!
console.log(str7.substring(6,11)) //happy
  • substr:在字符串中抽取从 start 下标开始的指定数目的字符。
console.log(str7.substr(6)) //happy world!
console.log(str7.substr(6,11)) //happy world

split

  • split:把一个字符串分割成字符串数组。
var str8="How are you doing today?"
console.log(str8.split(''))
console.log(str8.split(' ',3))

toLocaleLowerCase() 和toLocaleUpperCase() 和toLowerCase() 和toUpperCase()

  • toLocaleLowerCase:把字符串转换为小写。
    与 toLowerCase() 不同的是,toLocaleLowerCase() 方法按照本地方式把字符串转换为小写。只有几种语言(如土耳其语)具有地方特有的大小写映射,所有该方法的返回值通常与 toLowerCase() 一样。
  • toLocaleUpperCase:把字符串转换为大写。
  • toLowerCase: 把字符串转换为小写。
  • toUpperCase:把字符串转换为大写。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值