字符串的常用方法
●我們操作字符串,也有一堆的方法來幫助我們操作
●字符串和數(shù)組有一個一樣的地方,也是按照索引來排列的
●注意:所有字符串常用方法, 都不會改變原始字符串, 都是以返回值的形式出現(xiàn)結(jié)果
字符串常用方法之 charAt
●作用:charAt() 是找到字符串中指定索引位置的內(nèi)容返回
●語法:字符串.charAt(索引)
●返回值:該索引位置對應(yīng)的字符
○如果有該索引位置, 那么就是該索引位置的字符
○如果沒有該索引位置, 那么就是 空字符串('')
var str = 'hello world'
// 使用 charAt 找到字符串中的某一個內(nèi)容
var index = str.charAt(2)
console.log(index) // l
//查找索引為 13 的內(nèi)容,因?yàn)闆]有返回是一共空字符串
var index1 = str.charAt(13)
console.log(index1); // ''
字符串常用方法之 charCodeAt
●作用:charCodeAt() 就是返回對應(yīng)索引位置的 unicode 編碼
●語法:字符串.charCodeAt(索引)
●返回值:該索引位置的對應(yīng)字符的 編碼(十進(jìn)制)
var str = 'hello world'
// 使用 charAt 找到字符串中的某一個內(nèi)容
var index = str.charCodeAt(4)
console.log(index) // 111
○因?yàn)?0 在 unicode 對照表里面存儲的是 111,所以就會返回 111
字符串常用方法之 indexOf
●作用:indexOf 就是按照字符找到對應(yīng)的索引
●語法:字符串.indexOf(要查找的字符,開始索引)
●返回值:
○如果有該字符內(nèi)容, 那么就是該字符的索引位置
○如果沒有該字符內(nèi)容, 就是 -1
var str = 'hello world'
// 使用 indexOf 找到字符串中的某一個內(nèi)容
var index = str.indexOf('l', 0)
console.log(index) // 2 返回第一個找到的內(nèi)容的下標(biāo)后面的就不查找了
var index1 = str.indexOf('w', 3)
console.log(index1); // 6 不管從那個索引開始,索引的位置不變
var index2 = str.indexOf('w', 7)
console.log(index2); // -1 從索引7開始查找沒有找到返回-1
字符串常用方法之 lastIndexOf
●作用:lastIndexOf 是從后向前檢測該字符在字符串內(nèi)的索引位置
●語法:字符串.indexOf(要查找的字符,開始索引)
●返回值:
○如果有該字符內(nèi)容, 那么就是該字符的索引位置
○如果沒有該字符內(nèi)容, 就是 -1
var str = 'hello world'
// 使用 lastIndexOf 找到字符串中的某一個內(nèi)容
var index = str.lastIndexOf('l')
console.log(index) //9 返回第一個找到的內(nèi)容的下標(biāo)后面的就不查找了,索引的位置不變
var index = str.lastIndexOf('l', 8)
console.log(index) //3 返回第一個找到的內(nèi)容的下標(biāo)后面的就不查找了,索引的位置不變
var index = str.lastIndexOf('w', 5)
console.log(index) //-1 從后開始查找,開始的索引是5 但是前面沒有找到w 返回-1
字符串常用方法之 substring
●作用:substring 是用來截取字符串使用的
●語法: substring(從哪個索引開始,到哪個索引截止),包含開始索引,不包含結(jié)束索引
●返回值:返回截取到的內(nèi)容
var str = 'hello world'
// 使用 substring截取字符串中的某一個內(nèi)容
var res = str.substring(2, 8)
console.log(res); //llo wo
字符串常用方法之 substr
●作用:substr 也是用來截取字符串的
●語法:substr(從哪個索引開始,截取多少個)
●返回值:截取到的內(nèi)容
var str = 'hello world'
// 使用 substr截取字符串中的某一個內(nèi)容
var res = str.substr(2, 7)//從索引2開始,截取7個
console.log(res); //llo wor
○這個方法和 substring 不一樣的是,第二個參數(shù)是截取多少個
字符串常用方法之 toLowerCase 和 toUpperCase
●作用:這兩個方法分別是用來給字母格式的字符串轉(zhuǎn)成 小寫字母 和 大寫字母 的
●語法:
○字符串.toLowerCase()
○字符串.toUpperCase()
var str = 'hello world'
// 使用 toUpperCase 轉(zhuǎn)換成大寫
var upper = str.toUpperCase()
console.log(upper) // HELLO WORLD
// 使用 toLowerCase 轉(zhuǎn)換成小寫
var lower = upper.toLowerCase()
console.log(lower) // hello world
字符串常用方法之 slice
●作用:截取字符串
●語法:字符串.slice(起始索引,結(jié)束索引)
○包含開始的索引對應(yīng)的內(nèi)容,不包含結(jié)束索引對應(yīng)的內(nèi)容
○結(jié)束索引不寫就直接截取到末尾
●返回值:截取出來的字符串
var str = 'hello world'
// 使用 slice 截取字符串
var res = str.slice(1, 4) //ell
console.log(res);
//沒有結(jié)束的索引直接截取到末尾
var res1 = str.slice(1) //ello world
console.log(res1);
字符串常用方法之 replace
●作用:用指定的內(nèi)容替換掉字符串中的內(nèi)容
●語法:字符串.repalce(被替換的內(nèi)容,要替換的內(nèi)容)
○被替換內(nèi)容 => 換下內(nèi)容
○要替換內(nèi)容 => 換上內(nèi)容
●返回值:替換好的字符串
●注意:內(nèi)容只能被替換一次,從索引0 的位置開始
var str = 'hello world'
// 使用 replace 替換字符串中的內(nèi)容
var res = str.replace('l', 'M')
console.log(res); // heMlo world
console.log(str); // hello world
字符串常用方法之 split
●作用:按照切割符號, 把字符串切割開, 放在一個數(shù)組里面.
●語法:字符串.split('指定的切割符')
○切割符可以不傳遞,就會和整體當(dāng)做一個字符串
○('')空字符串會一位一位的切割
○(' ') 字符串中有空格 會按照原字符串中的空格切割
●返回值:一個用指定切割符切割好的數(shù)組
var str = 'hello world'
// 使用 split 切割成一個數(shù)組
var res = str.split()
console.log(res); //['hello world']
var res1 = str.split('')
console.log(res1); //['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
var res2 = str.split(' ')
console.log(res2); //['hello', 'world']
字符串常用方法之 concat
●作用:字符串拼接也可以說是字符串合并
●語法:字符串.concat(字符串)
●返回值:拼接后的字符串
var str = 'hello world '
var str1 = 'ni hao'
// 使用 concat 切割成一個數(shù)組
var res = str.concat('ni hao')
console.log(res); // hello world ni hao
var res1 = str.concat(str1)
console.log(res1); // hello world ni hao
字符串常用方法之 trim
●作用:取出字符串頭尾的空白內(nèi)容
●語法:字符串.trim()
●返回值:去除空白內(nèi)容以后的字符串
var str = ' hello world '
// 使用 trim 切割成一個數(shù)組
var res = str.trim()
console.log(res); // hello world
字符串常用方法之 trimStart / trimLeft
●作用:去除字符串頭部的空白內(nèi)容
●語法:
○字符串.trimStart()
○字符串.trimLeft()
●返回值:去除空白內(nèi)容以后的字符串文章來源:http://www.zghlxwxcb.cn/news/detail-584075.html
var str = ' hello world '
// 使用 trimStart 后者trimLeft去除頭部的空白內(nèi)容
var res = str.trimStart()
console.log(res); //hello world
var res1 = str.trimLeft()
console.log(res1); //hello world
字符串常用方法之 trimEnd / trimRight
●作用:去除字符串尾部的空白內(nèi)容
●語法:
○字符串.trimtrimEnd()
○字符串.trimRight()
●返回值:去除空白內(nèi)容以后的字符串文章來源地址http://www.zghlxwxcb.cn/news/detail-584075.html
var str = ' hello world '
// 使用 trimEnd 后者trimRight去除尾部的空白內(nèi)容
var res = str.trimEnd()
console.log(res); // hello world
var res1 = str.trimRight()
console.log(res1); // hello world
到了這里,關(guān)于JavaScript字符串常用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!