文章目錄
Js 字符串 常用方法總結
字符串查找相關方法
includes() 方法
**查找字符串中是否包含某個字符或字符串, 返回布爾值
true
或false
** , 區分大小寫語法:
str.includes(searchString[, position])
參數:
searchString
要在此字符串中查找的字符串position
| 可選, 從當前字符串的哪個索引位置開始搜索字符串, 默認爲0.返回值: 布爾值,
true
或false
const str = 'yangyang ni hao MA' console.log(str.includes('yang')); // true console.log(str.includes('Yang')); // false 區分大小寫
indexOf() 方法
返回索引
indexOf()
方法返回調用它的String
對象中第一次出現的指定值的索引, 從fromIndex
處進行搜索, 如果未找到該值, 則返回-1
, 該方法常用來判斷一個字符串裏是否包含某個子字符串, 如果返回-1
則證明沒有, 但是現在用includes()
方法更簡單.語法:
str.indexOf(searchValue[,fromIndex])
參數:
searchValue
要被查找的值.fromIndex | 可選
開始查找的位置, 可以是任意整數, 默認爲0
.返回值:
查找的字符串
searchValue
的第一次出現的索引, 如果沒有, 則返回-1
.const str = 'good good study, day day up!'; console.log(str.indexOf('day')); // 17 匹配的第一個位置索引 console.log(str.indexOf('Gene')); // 沒有該字符返回 -1
lastIndexOf() 方法
lastIndexOf()
方法區分大小寫匹配.
lastIndexOf()
方法返回調用String
對象指定值最後一次出現的索引, 在一個字符串中的指定位置fromIndex
處從後向前索引, 如果沒找到這個特定值則返回-1
.該方法將從尾到頭的檢索字符串
str
, 看它是否含有子串, 如果有, 返回符合條件的子串的第一個位置語法:
str.lastIndexOf(searchValue[, fromIndex])
參數:
searchValue
一個字符串, 表示被查找的結果. 如果searchValue
是空串, 則返回fromIndex
fromIndex
| 可選.返回值:
返回指定值最後一次出現的索引(即: 從右向左第一次查到的字符的索引)(索引仍是以從左至右0開始記數的), 如果沒找到則返回
-1
.代碼案例:
# lastIndexOf() 該方法通常可以用來截取文件的後綴名比較常用 const str = "員工工.作表.xls" console.log(str.lastIndexOf(',')); // 沒有返回 -1 console.log(str.lastIndexOf('.')); // 返回第一次查找到的字符 索引位置 console.log(str.substring(str.lastIndexOf('.')+1)); // 可以用來截取文件後綴名 xls
charAt() 方法
**
charAt()
方法從一個字符串中返回指定位置的字符. **語法:
str.charAt(index)
參數: 一個介於 0 和字符長度減 1 之間的整數, 如果沒有提供索引,
charAt()
將使用0
.const str = '楊陽,你好啊' console.log(str.charAt(3)) // 你
獲取或截取字符方法
str[] 獲取字符
可以通過
str[index]
的方式獲取字符串對應索引位置的字符,一般獲取第一位字符時, 這樣比較便捷.const str = '你好啊, 最近忙嗎?' const result = str[0] console.log(result) // 你 -->> 可以方便的獲取到第一個字符
substring() 方法
描述:
substring
提取從indexStart
到indexEnd
(不包括結束位置)之間的字符**語法: **
str.substring(indexStart[, indexEnd])
參數:
indexStart
需要截取的第一個字符的索引indexEnd
| 可選, 一個 0 到 字符串長度之間的整數返回值: 截取返回的新的子字符串.
const str = 'good good study, day day up!'; console.log(str.substring(3, 8)); // d goo console.log(str); // 不改變原來的字符串
slice() 方法
slice
方法提取某個字符串的一部分, 並且返回新的字符串,且不改變原字符串語法:
str.slice(beginIndex[, endIndex])
參數:
beginIndex
截取字符串的起始位置endIndex
截取字符串的結束位置返回值: 一個新的從原始字符串截取出來的字符串.
const str = 'good good study, day day up!'; console.log(str.slice(3,9)); // d good
去除首尾空字符串方法
trim()方法
語法:
str.trim()
返回值: 去除了開頭和結尾空字符串的字符串.
const str = ' hello 你好! ' console.log(str.length); // 長度爲20 console.log(str.trim().length); // 去除首尾空格 長度爲 9
trimStart()/trimLeft() 方法
trimStart/trimLeft
方法刪除字符串開頭的空格.
語法:
str.trimStart() / str.trimLeft()
返回值: 一個新的刪除了開頭空格的字符串, 不改變原來字符串的值.
const str = ' hello,你好哇! ' console.log(str.length); // 18 console.log(str.trimStart().length); // 17 去除了開頭 1個空格
trimEnd() /trimRight() 方法
去除字符串末尾的空格, 多數用
trimEnd()
方法.
語法:
str.trimEnd() 或 str.trimRight()
返回值: 新字符串, 去除了末尾空格
const str = ' hello,你好哇! ' console.log(str.length); // 18 console.log(str.trimEnd().length); // 11 去除了7個空格
其他相關方法
split() 把字符串分割成字符數組方法
split()
方法用來把字符串分割成 字符數組, 用一個指定的分隔符來分割字符串.
語法:
str.split([separator[, limit]])
參數:
separator
指定按照什麼進行拆分字符串,separator
可以是一個字符串, 也可以是一個 正則表達式, 如果純文本分隔符包含多個字符, 則必須找到整個字符串來作爲分割點. 如果在str
中省略, 或者不出現分隔符, 則返回的數組包含一個由整個字符串組成的元素, 如果分隔字符串爲空字符串, 則返回每一個字符以數組形式返回limit
一個整數, 限定返回的分割片段數量. 當提供此參數時,split 方法會在指定分隔符的每次出現時分割該字符串,但在限制條目已放入數組時停止。如果在達到指定限制之前達到字符串的末尾,它可能仍然包含少於限制的條目。新數組中不返回剩下的文本。返回值: 返回按照指定分隔符分割形成的字符數組
const str = 'good good study, day day up!'; console.log(str.split()); // ["good good study, day day up!"] 不separator console.log(str.split('')); // 空字符串 console.log(str.split(',')); // ["good good study", " day day up!"] 逗號分隔
replace() 方法
replace()
方法返回一個替換之後的新字符串, 原字符串不會改變.
語法:
str.replace(regexp|substr, newSubStr|function)
參數:
regexp(pattern)
一個RegExp
正則對象, 或者正則字面量. 該正則所匹配的內容會被第二個參數的返回值替換掉.subStr(pattern)
一個將被newSubStr
替換的字符串. 其被視爲一整個字符串, 而不是正則表達式, 僅第一個匹配項會被替換.newSubString(replacement)
用於替換掉第一個參數在原字符串中匹配部分的字符串
, 該字符串可以內查一些特殊的變量名.function(replacement)
一個用來創建新字符串的函數, 該函數的返回值將替換掉第一個參數匹配到的結果.返回值: 一個部分或全部匹配由替代模式所取代的新的字符串。原字符串不會改變.
const str = 'good good study, day day up!'; // // 正則匹配方式 console.log(str.replace(/good/, 'GOOD')); // 默認值匹配第一個 GOOD good study, day day up! console.log(str.replace(/good/g, 'GOOD')); // 正則添加 g 全局匹配替換 GOOD GOOD study, day day up! // 字符串匹配方式 console.log(str.replace('day', 'DAY')); // 還是隻能匹配第一個 good good study, DAY day up! // 函數替換 /* * match 匹配的子字符串 * $1,$2...如果replace()方法第一個參數是正則對象,有分組的話,則代表分組內匹配的字符串 * offset 匹配的字符串的索引偏移量 * string 被匹配的原始字符串 * */ const result = str.replace(/\b([a-zA-Z])[a-zA-Z]*\b/g, (match, $1, offset, string) => { console.log(match, $1, offset, string); $1 = $1.toUpperCase() return $1 + match.substring(1) // 實現首字母轉換成大寫字母 }) console.log(result);
startsWith() 方法
判斷一個字符串是否以某個字符串開始, 返回布爾值
true
或false
, 該方法區分大小寫
語法:
str.startsWit(searchString[,position])
參數:
searchString
要搜索的字符串position
| 可選 在str
中搜索searchString
的開始位置,默認值是0.const str = 'good good study, day day up!'; console.log(str.startsWith('good')); // true console.log(str.startsWith('Good')); // false
endsWith() 方法
endsWith()
方法用來判斷當前字符串是否以某個字符串結束, 返回true
或false
語法:
str.endsWith(searchString[, length])
參數:
searchString
要搜索的字符串length | 可選
: 作爲str
的長度, 默認值爲str.length
返回值: 如果是以指定字符串結尾, 返回
true
, 如果不是 返回false
.# endsWith() 方法區分大小寫 const str = 'hello world' console.log(str.endsWith('world')); // true console.log(str.endsWith('World')); // false
字符串
length | 可選
: 作爲str
的長度, 默認值爲str.length
返回值: 如果是以指定字符串結尾, 返回
true
, 如果不是 返回false
.# endsWith() 方法區分大小寫 const str = 'hello world' console.log(str.endsWith('world')); // true console.log(str.endsWith('World')); // false
總結
交流學習加V
Gene199302