js中的String字符串方法

创建字符串的方法
  • 字面量方式
let str = '123'
  • 类方法创建
let str = new String('123') 
console.log(str) // String {"123"}
// 当然可以通过valueOf或者toString方法把其转化成和字面量创建的一样
str.valueOf() // 或者str.toString()
console.log(str) // '123'

目录
slice方法
split方法
substring方法
charAt方法
charCodeAt方法
concat方法
codePointAt方法
endsWith方法
includes方法
indexOf方法
toUpperCase方法
toLowerCase方法
trim方法

String类私有方法
  • slice方法

     /*    方法:slice(start,end)
     *     功能:拷贝字符串中的某段数据,对原字符串不会产生改变
     *     返回值:返回拷贝了某段数据的新字符串
     *     参数:start:起始位,从0开始
     *                end(可选):结束位
     */
    
     let string = 'happy day'
     let stringSlice= string.slice(1,2) // 从位置1即第二个字符开始,拷贝到位置为2前
     console.log(string ,stringSlice) //string:'happy day'  stringSlice:'a'
     stringSlice= string .slice(0)// 拷贝整个字符串
     console.log(string ,stringSlice) //string:'happy day'  stringSlice:'happy day'
    
  • split方法

     /*    方法:split(separator, limit)
     *     功能:以指定的分割符对字符串进行分割
     *     返回值:返回以分隔符分割成的数组
     *     参数:separator:分隔符
     *                limit(可选):限制分割的份数
     */
    
     let string = 'happy day and day'
     let ArraySplit= string.split(' ') // 以空格进行分割
     console.log(string ,ArraySplit) //string:'happy day and day'  ArraySplit:['happy','day','and','day']
     ArraySplit= string.split(' ',2) // 以空格进行分割,并提取2份
     console.log(string ,ArraySplit) //string:'happy day and day'  ArraySplit:['happy','day']
    
  • substring方法

     /*    方法:substring(start, end)
     *     功能:截取指定位置的字符串
     *     返回值:返回截取到的字符串
     *     参数:start:起始位置,从0开始
     *                end(可选):结束位置
     */
    
     let string = 'happy day'
     let stringSubstring= string.substring(0,4) // 从开头位置截取到位置4之前
     console.log(string ,stringSubstring) //string:'happy day'  stringSubstring:'happ'
     stringSubstring= string.substring(5) // 从位置5开始截取到完
     console.log(string ,stringSubstring) //string:'happy day'  stringSubstring:'day'
    
  • charAt方法

     /*    方法:charAt(index)
     *     功能:从字符串中返回指定的字符
     *     返回值:返回的字符
     *     参数:index:指定位置,从0开始
     */
    
     let string = 'happy day'
     let char= string.charAt(4) // 读取第五个字符
     console.log(string ,char) //string:'happy day'  char:'y'
    
  • charCodeAt方法

     /*    方法:charCodeAt(index)
     *     功能:从字符串中返回指定字符的16位Unicode编码
     *     返回值:Unicode编码
     *     参数:index:指定位置,从0开始
     */
    
     let string = 'happy day'
     let charcode= string.charCodeAt(4) // 读取第五个字符
     console.log(string ,charcode) //string:'happy day'  charcode:121
    
  • concat方法

     /*    方法:concat(str1,str2,str3...)
     *     功能:字符串拼接
     *     返回值:返回拼接完的新数组
     *     参数:
     *             str1:拼接的字符串
     */
    
     let str1 = 'happy day'
     let str2 = 'and day'
     let str3 = str1.concat(str2) // 读取第五个字符
     console.log(str1,str2,str3 ) //str1:'happy day'  str2:'and day' str3:'happy dayand day'
     // 更建议使用[赋值操作符](`+`, `+=`)
    
    
  • codePointAt方法

     /*    方法:codePointAt(index)
     *     功能:从字符串中返回指定字符的Unicode编码
     *     返回值:Unicode编码
     *     参数:index:指定位置,从0开始
     */
    
     let string = 'happy day'
     let charcode= string.codePointAt(4) // 读取第五个字符
     console.log(string ,charcode) //string:'happy day'  charcode:121
     // 注意其与charCodeAt方法的区别:charCodeAt是用2个字节编码(码元),codePointAt使用4个字节编码(码点)
    
  • endsWith方法

     /*    方法:endsWith(str,index)
     *     功能:判断给定字符或者是字符串是否是该字符串的结尾
     *     返回值:true或false
     *     参数:str:字符串
     *                index(可选):把第几位当作是末尾(以1开始)
     */
    
     let string = 'happy day'
     let endwith= string.endsWith('ay') // 末尾是以’ay‘结尾
     console.log(string ,endwith) //string:'happy day'  charcode:true
     endwith= string.endsWith('app',4) // 把第四位当作是末尾,判断’app‘是不是结尾
     console.log(string ,endwith) //string:'happy day'  charcode:true
    
  • includes方法

     /*    方法:includes(searchStr,index)
     *     功能:查找字符串是否包含提供的字符串
     *     返回值:true或false
     *     参数:searchStr:查找的字符串
     *                index(可选):从第几位找起(从0开始)
     */
    
     let string = 'happy day'
     let includeFlag= string.includes('ay') // 查找是否存在'ay'
     console.log(string ,includeFlag) //string:'happy day'  includeFlag:true
     includeFlag= string.includes('day',5) // 从第六个字符开始查找是否存在’day'
     console.log(string ,includeFlag) //string:'happy day'  includeFlag: true
    
  • indexOf方法

     /*    方法:indexOf(searchStr,index)
     *     功能:查找某字符串在该字符串中第一次出现的索引位置
     *     返回值:索引位置
     *     参数:searchStr:查找的字符串
     *                index(可选):从第几位找起(从0开始)
     */
    
     let string = 'happy day'
     let indexof= string.indexOf('ppy') // 查找’ppy'在字符串中第一次出现的索引位置
     console.log(string ,indexof) //string:'happy day'  indexof: 2
     indexof= string.indexOf('ppy',2) // 从索引位置为2开始查找’ppy‘的索引位置
     console.log(string ,indexof) //string:'happy day'  indexof: 2
    
  • toUpperCase方法

     /*    方法:toUpperCase()
     *     功能:把字符串转化为大写
     *     返回值:转换为大写的新字符串
     */
    
     let string = 'happy day'
     let upperCase= string.toUpperCase() // 把字符串转换成大写
     console.log(string ,upperCase) //string:'happy day'  upperCase: 'HAPPY DAY'
    
  • toLowerCase方法

     /*    方法:toLowerCase()
     *     功能:把字符串转化为小写
     *     返回值:转换为小写的新字符串
     */
    
     let string = 'HAPPY DAY'
     let upperCase= string.toLowerCase() // 把字符串转换成小写
     console.log(string ,upperCase) //string:'HAPPY DAY'  upperCase: 'happy day'
    
  • trim方法

     /*    方法:trim()
     *     功能:去掉字符串两端的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR等)
     *     返回值:返回去掉空白字符的字符串
     */
    
     let string = '  happy day   '
     let trimstr= string.trim() // 去掉字符串两端的空白字符
     console.log('string:' ,string ,'trimstr:',trimstr) //string:'  happy day   '  trimstr: 'happy day'
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值