js中常用字符串方法總結

  • includes()
判斷一個字符串是否包含其他字符串
// 返回布爾值, 表示是否找到了參數字符串
let s = 'Hello world!'
s.includes('0') // true
// 支持第二個參數,表示開始搜索的位置
s.includes('Hello', 6) // false
  • endsWith()
判斷一個字符串的結尾是否包含其他字符串的字符
// 返回布爾值, 表示字符串是否在原字符串的尾部
let s = 'Hello world!'
s.endsWith('!') // true
// 支持第二個參數,表示開始搜索的位置
s.endsWith('Hello', 5) // true
  • indexOf()
從字符串對象中返回首個被發現的給定值的索引值,如果沒有找到則返回-1
let s = 'Hello world!'
s.indexOf('o') // 4
  • lastIndexOf()
從字符串中返回最後一個被發現的給定值的索引值, 如果沒有找到則返回-1
let s = 'Hello world'
s.lastIndexOf('o') // 7
  • match()
使用正則表達式與字符串相比較
var str="1 plus 2 equal 3"
console.log(str.match(/\d+/g)) // [1, 2, 3]
  • padEnd()、padStart()
在當前字符串尾部、頭部填充指定的字符串, 直到達到指定的長度. 返回一個新字符串
// padStart()用於頭部補全, padEnd()用於尾部補全, 一共接受兩個參數, 第一個參數用來指定字符串的最小長度, 第二參數是用來補全的字符串
'x'.padStart(5, 'ab') // 'ababx'
'x'.padEnd(5, 'ab') // 'xabab'
// 如果原字符串的長度大雨或等於指定的最小長度, 則返回原字符串
'xxx'.padStart(2, 'ab') // 'xxx'
'xxx'.padEnd(2, 'ab') // 'xxx'
// 如果用來補全的字符串與原字符串,兩者的長度之和超過了指定的最小長度, 則會截去超出位數的補全字符串
'xxx'.padStart(10, '0123456789') // '0123456xxx'
'xxx'.padEnd(10, '0123456789') // 'xxx0123456'
// 如果省略第二個參數, 默認使用空分補全長度
'x'.padStart(4) // '   x'
'x'.padEnd(4) // 'x   '
// 另一個用途是提示字符串格式
'12'.padStart(10, 'YYYY-MM-DD') // 'YYYY-MM-12'
'09-12'.padStart(10, 'YYYY-MM-DD') // 'YYYY-09-12'
  • repeat()
返回指定重複次數的元素組成的字符串對象
// repeat方法返回一個新字符串, 表示將原字符串重複n次
'x'.repeat(3) // 'xxx'
// 參數如果是小數,會被向下取整
'x'.repeat(2.9) // 'xx'
// 如果repeat的參數是負數或者Infinity, 會報錯
'x'.reapeat(Infinity) // RangeError
'x'.repeat(-1) //RangeError
// 如果參數是-1到0之間的小數或者是NaN,則等同於0
'x'.repeat(-0.9) // ''
'x'.repeat(NaN) // ''
// 如果參數是字符串,則會先轉換成數字
'x'.repeat('x') // ''
'x'.repeat('3') // 'xxx'
  • replace()
被用來在正則表達式和字符串直接比較, 然後用新的子串來替換被匹配的子串
var str = '我我我我我愛中國!'
console.log(str.replace(/我/g, '你')) // 你你你你你愛中國!
  • search()
對正則表達式和指定字符串進行匹配搜索, 返回第一個出現的匹配項的下標
var str = 'Hello world!'
console.log(str.search(/world/)) // 6
  • slice()
摘取一個字符串區域,返回一個新的字符串
var str="Hello happy world!"
console.log(str.slice(6)) // happy world! 傳一個參數,表示從該索引開始截取至最後
console.log(str.slice(6, 11)) // happy 傳兩個參數,第二個參數表示到該索之前引截止
  • split()
通過分離字符串成字串, 將字符串對象分割成字符串數組
var str = 'what are you doing now?'
var s = str.split(' ')
console.log(s) // ['what', 'are', 'you', 'doing', 'now']
var t = str.split(' ', 3)
console.log(t) // ['what', 'are', 'you'] 第二個參數表示前幾個
  • startsWith()
判斷字符串的起始位置是否匹配其他字符串中的字符
// 返回布爾值, 表示參數字符串是否在原字符串的頭部
let s = 'Hello world!'
s.startsWith('Hello') // true
// 支持第二個參數,表示開始搜索的位置
s.startsWith('world', 6) // true
  • substr()
通過指定字符數返回在指定位置開始的字符串中的字符
var str = 'Hello world!'
var s = str.substr(3)
console.log(s) // 'lo world!'
var t = str.substr(3, 7)
console.log(t) // 'lo worl'
  • substring()
返回在字符串中指定兩個下邊之間的字符
var str="Hello world!"
console.log(str.substring(3)) // 'lo world!'
consoel.log(str.substring(3, 7)) // 'lo w'
  • toLowerCase()
把字符串轉換成小寫並返回
var str = 'HELLo WorlD'
console.log(str.toLowerCase()) // 'hello world'
  • toUpperCase()
把字符串轉換成大寫並返回
var str = 'HELLo WorlD'
console.log(str.toUpperCase()) // 'HELLO WORLD'
  • trim()
從字符串的開始和結尾去除空格
var str = '   123   abc    '
console.log(str.trim()) // '123   abc'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章