亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript字符串常用屬性方法匯總及詳解

 更新時(shí)間:2025年10月22日 09:01:51   作者:走粥  
在JavaScript中,字符串是一個(gè)非常重要的數(shù)據(jù)類型,這篇文章主要介紹了JavaScript字符串常用屬性方法匯總及詳解的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

方法分類匯總索引

  1. 獲取字符串信息方法
  • length:返回字符串的長度(字符數(shù)量)
  1. 字符串查找方法
  • charAt():返回指定索引位置的字符
  • charCodeAt():返回指定索引位置字符的 Unicode 編碼
  • indexOf():查找子字符串首次出現(xiàn)的位置
  • lastIndexOf():查找子字符串最后出現(xiàn)的位置
  • includes():判斷是否包含指定子字符串
  • startsWith():判斷是否以指定子字符串開頭
  • endsWith():判斷是否以指定子字符串結(jié)尾
  1. 字符串截取/提取方法
  • slice():提取字符串的一部分并返回新字符串
  • substring():提取兩個(gè)指定索引之間的字符
  • substr():從指定位置提取指定長度的子字符串(不推薦使用)
  1. 字符串轉(zhuǎn)換方法
  • toLowerCase():將字符串轉(zhuǎn)換為小寫
  • toUpperCase():將字符串轉(zhuǎn)換為大寫
  • toString():返回字符串本身
  • valueOf():返回字符串的原始值
  1. 字符串替換/分割方法
  • replace():查找匹配子字符串并替換為新內(nèi)容
  • split():將字符串分割成字符串?dāng)?shù)組
  1. 字符串修剪方法
  • trim():去除字符串兩端的空白字符
  • trimStart()/trimLeft():去除字符串開頭的空白字符
  • trimEnd()/trimRight():去除字符串結(jié)尾的空白字符
  1. 字符串重復(fù)方法
  • repeat():將字符串重復(fù)指定的次數(shù)
  1. 字符串連接方法
  • concat():連接兩個(gè)或多個(gè)字符串
  1. 字符串填充方法
  • padStart():在字符串開頭填充字符至指定長度
  • padEnd():在字符串結(jié)尾填充字符至指定長度
  1. 其他字符串方法
  • match():查找匹配正則表達(dá)式的結(jié)果
  • matchAll():返回所有匹配正則表達(dá)式的迭代器
  • search():查找與正則表達(dá)式匹配的子字符串位置
  • localeCompare():考慮區(qū)域設(shè)置比較兩個(gè)字符串

以下為 JavaScript 中常用的字符串方法詳解,這些方法都不會改變原字符串,而是返回一個(gè)新的字符串或其他類型的值。

一、獲取字符串信息屬性

1. length

  • 作用:返回字符串的長度(字符數(shù)量)
  • 語法str.length
  • 參數(shù):無參數(shù)
  • 返回值:數(shù)字,表示字符串的長度
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 獲取字符串長度
const len = str.length;
console.log(len); // 輸出: 11
// 注意:length是屬性,不是函數(shù),不需要加括號

二、字符串查找方法

1. charAt()

  • 作用:返回指定索引位置的字符
  • 語法str.charAt(index)
  • 參數(shù)index - 必需,表示字符在字符串中的索引(從0開始)
  • 返回值:字符串,指定索引處的字符;如果索引超出范圍,則返回空字符串
  • 是否改變原字符串:否
  • 示例
const str = "JavaScript";
// 獲取索引為4的字符
const char = str.charAt(4);
console.log(char); // 輸出: 'S'

// 獲取索引超出范圍的字符(返回空字符串)
const invalidChar = str.charAt(20);
console.log(invalidChar); // 輸出: ''

2. charCodeAt()

  • 作用:返回指定索引位置字符的 Unicode 編碼
  • 語法str.charCodeAt(index)
  • 參數(shù)index - 必需,字符在字符串中的索引(從0開始)
  • 返回值:數(shù)字,表示指定索引處字符的 Unicode 編碼;如果索引超出范圍,則返回 NaN
  • 是否改變原字符串:否
  • 示例
const str = "A";
// 獲取字符 'A' 的 Unicode 編碼
const code = str.charCodeAt(0);
console.log(code); // 輸出: 65

// 獲取索引超出范圍的字符編碼(返回NaN)
const invalidCode = str.charCodeAt(1);
console.log(invalidCode); // 輸出: NaN

3. indexOf()

  • 作用:查找某個(gè)子字符串在當(dāng)前字符串中首次出現(xiàn)的位置
  • 語法str.indexOf(searchValue[, fromIndex])
  • 參數(shù)
    • searchValue - 必需,要查找的子字符串
    • fromIndex - 可選,開始查找的索引位置,默認(rèn)為0
  • 返回值:數(shù)字,子字符串首次出現(xiàn)的索引;如果沒有找到,則返回-1
  • 是否改變原字符串:否
  • 示例
const str = "Hello, Hello World";
// 查找 "Hello" 首次出現(xiàn)的位置
const firstPos = str.indexOf("Hello");
console.log(firstPos); // 輸出: 0

// 從索引5開始查找 "Hello"
const posFrom5 = str.indexOf("Hello", 5);
console.log(posFrom5); // 輸出: 7

// 查找不存在的子字符串
const notFound = str.indexOf("JavaScript");
console.log(notFound); // 輸出: -1

4. lastIndexOf()

  • 作用:查找某個(gè)子字符串在當(dāng)前字符串中最后出現(xiàn)的位置
  • 語法str.lastIndexOf(searchValue[, fromIndex])
  • 參數(shù)
    • searchValue - 必需,要查找的子字符串
    • fromIndex - 可選,開始查找的索引位置,默認(rèn)為字符串的長度-1
  • 返回值:數(shù)字,子字符串最后出現(xiàn)的索引;如果沒有找到,則返回-1
  • 是否改變原字符串:否
  • 示例
const str = "Hello, Hello World";
// 查找 "Hello" 最后出現(xiàn)的位置
const lastPos = str.lastIndexOf("Hello");
console.log(lastPos); // 輸出: 7

// 在索引6之前查找 "Hello"
const posBefore6 = str.lastIndexOf("Hello", 6);
console.log(posBefore6); // 輸出: 0

5. includes()

  • 作用:判斷當(dāng)前字符串是否包含指定的子字符串
  • 語法str.includes(searchValue[, fromIndex])
  • 參數(shù)
    • searchValue - 必需,要查找的子字符串
    • fromIndex - 可選,開始查找的索引位置,默認(rèn)為0
  • 返回值:布爾值,如果包含則返回true,否則返回false
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 判斷字符串是否包含 "World"
const hasWorld = str.includes("World");
console.log(hasWorld); // 輸出: true

// 從索引6開始查找 "World"
const hasWorldFrom6 = str.includes("World", 6);
console.log(hasWorldFrom6); // 輸出: true

// 判斷字符串是否包含 "Java"
const hasJava = str.includes("Java");
console.log(hasJava); // 輸出: false

6. startsWith()

  • 作用:判斷當(dāng)前字符串是否以指定的子字符串開頭
  • 語法str.startsWith(searchValue[, position])
  • 參數(shù)
    • searchValue - 必需,要查找的子字符串
    • position - 可選,開始查找的位置,默認(rèn)為0
  • 返回值:布爾值,如果以指定子字符串開頭則返回true,否則返回false
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 判斷字符串是否以 "Hello" 開頭
const startsWithHello = str.startsWith("Hello");
console.log(startsWithHello); // 輸出: true

// 判斷從索引6開始的子字符串是否以 "W" 開頭
const startsWithW = str.startsWith("W", 6);
console.log(startsWithW); // 輸出: true

// 判斷字符串是否以 "World" 開頭
const startsWithWorld = str.startsWith("World");
console.log(startsWithWorld); // 輸出: false

7. endsWith()

  • 作用:判斷當(dāng)前字符串是否以指定的子字符串結(jié)尾
  • 語法str.endsWith(searchValue[, length])
  • 參數(shù)
    • searchValue - 必需,要查找的子字符串
    • length - 可選,作為字符串長度使用的值,默認(rèn)為字符串的實(shí)際長度
  • 返回值:布爾值,如果以指定子字符串結(jié)尾則返回true,否則返回false
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 判斷字符串是否以 "World" 結(jié)尾
const endsWithWorld = str.endsWith("World");
console.log(endsWithWorld); // 輸出: true

// 只考慮前5個(gè)字符,判斷是否以 "Hello" 結(jié)尾
const endsWithHello = str.endsWith("Hello", 5);
console.log(endsWithHello); // 輸出: true

// 判斷字符串是否以 "Hello" 結(jié)尾
const endsWithHelloFull = str.endsWith("Hello");
console.log(endsWithHelloFull); // 輸出: false

三、字符串截取/提取方法

1. slice()

  • 作用:提取字符串的一部分,并返回一個(gè)新的字符串
  • 語法str.slice(startIndex[, endIndex])
  • 參數(shù)
    • startIndex - 必需,提取的起始索引(包含)
    • endIndex - 可選,提取的結(jié)束索引(不包含),默認(rèn)為字符串的長度
  • 返回值:字符串,提取的子字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 提取從索引6開始到結(jié)束的子字符串
const world = str.slice(6);
console.log(world); // 輸出: "World"

// 提取從索引0到5的子字符串(不包含索引5)
const hello = str.slice(0, 5);
console.log(hello); // 輸出: "Hello"

// 使用負(fù)數(shù)索引(從字符串末尾開始計(jì)算)
const last3 = str.slice(-3);
console.log(last3); // 輸出: "rld"

2. substring()

  • 作用:提取字符串中介于兩個(gè)指定索引之間的字符
  • 語法str.substring(indexStart[, indexEnd])
  • 參數(shù)
    • indexStart - 必需,提取的起始索引(包含)
    • indexEnd - 可選,提取的結(jié)束索引(不包含),默認(rèn)為字符串的長度
  • 返回值:字符串,提取的子字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 提取從索引0到5的子字符串
const hello = str.substring(0, 5);
console.log(hello); // 輸出: "Hello"

// 如果startIndex大于endIndex,會自動交換它們
const swapped = str.substring(5, 0);
console.log(swapped); // 輸出: "Hello"

// 負(fù)數(shù)索引會被視為0
const negative = str.substring(-3, 5);
console.log(negative); // 輸出: "Hello"

3. substr()

  • 作用:從指定位置開始提取指定長度的子字符串(注意:該方法已不推薦使用)
  • 語法str.substr(start[, length])
  • 參數(shù)
    • start - 必需,提取的起始索引
    • length - 可選,要提取的字符數(shù),默認(rèn)為到字符串的結(jié)尾
  • 返回值:字符串,提取的子字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 從索引6開始提取5個(gè)字符
const world = str.substr(6, 5);
console.log(world); // 輸出: "World"

// 從索引0開始提取5個(gè)字符
const hello = str.substr(0, 5);
console.log(hello); // 輸出: "Hello"

// 使用負(fù)數(shù)作為起始索引(從末尾開始計(jì)算)
const last3 = str.substr(-3);
console.log(last3); // 輸出: "rld"

四、字符串轉(zhuǎn)換方法

1. toLowerCase()

  • 作用:將字符串轉(zhuǎn)換為小寫
  • 語法str.toLowerCase()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,轉(zhuǎn)換為小寫的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 將字符串轉(zhuǎn)換為小寫
const lowerStr = str.toLowerCase();
console.log(lowerStr); // 輸出: "hello world"
console.log(str); // 輸出: "Hello World"(原字符串未改變)

2. toUpperCase()

  • 作用:將字符串轉(zhuǎn)換為大寫
  • 語法str.toUpperCase()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,轉(zhuǎn)換為大寫的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 將字符串轉(zhuǎn)換為大寫
const upperStr = str.toUpperCase();
console.log(upperStr); // 輸出: "HELLO WORLD"
console.log(str); // 輸出: "Hello World"(原字符串未改變)

3. toString()

  • 作用:返回字符串本身
  • 語法str.toString()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,與原字符串相同
  • 是否改變原字符串:否
  • 示例
const str = "Hello";
// 返回字符串本身
const sameStr = str.toString();
console.log(sameStr); // 輸出: "Hello"

// 對于String對象特別有用
const strObj = new String("World");
console.log(typeof strObj); // 輸出: "object"
const strVal = strObj.toString();
console.log(typeof strVal); // 輸出: "string"

4. valueOf()

  • 作用:返回字符串的原始值
  • 語法str.valueOf()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,字符串的原始值
  • 是否改變原字符串:否
  • 示例
// 對于字符串字面量,與原字符串相同
const str = "Hello";
console.log(str.valueOf()); // 輸出: "Hello"

// 對于String對象,返回其原始值
const strObj = new String("World");
console.log(strObj.valueOf()); // 輸出: "World"
console.log(typeof strObj.valueOf()); // 輸出: "string"

五、字符串替換/分割方法

1. replace()

  • 作用:在字符串中查找匹配的子字符串,并替換為新的子字符串
  • 語法str.replace(regexp|substr, newSubstr|function)
  • 參數(shù)
    • 第一個(gè)參數(shù):可以是字符串或正則表達(dá)式,表示要查找的內(nèi)容
    • 第二個(gè)參數(shù):可以是字符串或函數(shù),表示替換的內(nèi)容
  • 返回值:字符串,替換后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hello World, Hello JavaScript";

// 替換第一個(gè)匹配的子字符串
const replacedOnce = str.replace("Hello", "Hi");
console.log(replacedOnce); // 輸出: "Hi World, Hello JavaScript"

// 使用正則表達(dá)式替換所有匹配項(xiàng)(g表示全局匹配)
const replacedAll = str.replace(/Hello/g, "Hi");
console.log(replacedAll); // 輸出: "Hi World, Hi JavaScript"

// 使用函數(shù)作為替換值
const replacedWithFunc = str.replace(/Hello/g, (match) => {
  return match.toUpperCase();
});
console.log(replacedWithFunc); // 輸出: "HELLO World, HELLO JavaScript"

2. split()

  • 作用:將字符串分割成字符串?dāng)?shù)組
  • 語法str.split([separator[, limit]])
  • 參數(shù)
    • separator - 可選,字符串或正則表達(dá)式,用于指定分割的位置
    • limit - 可選,數(shù)字,限制返回的數(shù)組長度
  • 返回值:數(shù)組,分割后的字符串?dāng)?shù)組
  • 是否改變原字符串:否
  • 示例
const str = "apple,banana,orange,grape";

// 使用逗號作為分隔符
const fruits = str.split(",");
console.log(fruits); // 輸出: ["apple", "banana", "orange", "grape"]

// 限制返回的數(shù)組長度為2
const limitedFruits = str.split(",", 2);
console.log(limitedFruits); // 輸出: ["apple", "banana"]

// 使用空字符串作為分隔符,將每個(gè)字符作為數(shù)組元素
const chars = "hello".split("");
console.log(chars); // 輸出: ["h", "e", "l", "l", "o"]

六、字符串修剪方法

1. trim()

  • 作用:去除字符串兩端的空白字符(包括空格、制表符、換行符等)
  • 語法str.trim()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,去除兩端空白后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "   Hello World   ";
// 去除兩端空白
const trimmed = str.trim();
console.log(trimmed); // 輸出: "Hello World"
console.log(trimmed.length); // 輸出: 11(原長度為17)

2. trimStart() / trimLeft()

  • 作用:去除字符串開頭(左側(cè))的空白字符
  • 語法str.trimStart()str.trimLeft()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,去除開頭空白后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "   Hello World   ";
// 去除左側(cè)空白
const trimmedStart = str.trimStart();
console.log(trimmedStart); // 輸出: "Hello World   "
console.log(trimmedStart.length); // 輸出: 14(原長度為17)

// trimLeft是trimStart的別名
const trimmedLeft = str.trimLeft();
console.log(trimmedLeft); // 輸出: "Hello World   "

3. trimEnd() / trimRight()

  • 作用:去除字符串結(jié)尾(右側(cè))的空白字符
  • 語法str.trimEnd()str.trimRight()
  • 參數(shù):無參數(shù)
  • 返回值:字符串,去除結(jié)尾空白后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "   Hello World   ";
// 去除右側(cè)空白
const trimmedEnd = str.trimEnd();
console.log(trimmedEnd); // 輸出: "   Hello World"
console.log(trimmedEnd.length); // 輸出: 14(原長度為17)

// trimRight是trimEnd的別名
const trimmedRight = str.trimRight();
console.log(trimmedRight); // 輸出: "   Hello World"

七、字符串重復(fù)方法

1. repeat()

  • 作用:將字符串重復(fù)指定的次數(shù)
  • 語法str.repeat(count)
  • 參數(shù)count - 必需,數(shù)字,表示重復(fù)的次數(shù)(0到正無窮大之間的整數(shù))
  • 返回值:字符串,重復(fù)指定次數(shù)后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "Hi";
// 重復(fù)3次
const repeated = str.repeat(3);
console.log(repeated); // 輸出: "HiHiHi"

// 重復(fù)0次,返回空字符串
const empty = str.repeat(0);
console.log(empty); // 輸出: ""

// 重復(fù)次數(shù)為負(fù)數(shù)會報(bào)錯(cuò)
try {
  str.repeat(-1);
} catch (e) {
  console.log(e); // 輸出: RangeError: Invalid count value
}

八、字符串連接方法

1. concat()

  • 作用:連接兩個(gè)或多個(gè)字符串
  • 語法str.concat(str1[, str2[, ...[, strN]]])
  • 參數(shù)str1, str2, ..., strN - 要連接的字符串
  • 返回值:字符串,連接后的新字符串
  • 是否改變原字符串:否
  • 示例
const str1 = "Hello";
const str2 = " ";
const str3 = "World";

// 連接多個(gè)字符串
const result = str1.concat(str2, str3, "!");
console.log(result); // 輸出: "Hello World!"

// 與使用 + 運(yùn)算符效果相同
const sameResult = str1 + str2 + str3 + "!";
console.log(sameResult); // 輸出: "Hello World!"

九、字符串填充方法

1. padStart()

  • 作用:在字符串的開頭填充指定的字符,直到達(dá)到指定的長度
  • 語法str.padStart(targetLength[, padString])
  • 參數(shù)
    • targetLength - 必需,數(shù)字,目標(biāo)字符串的長度
    • padString - 可選,用于填充的字符串,默認(rèn)為空格
  • 返回值:字符串,填充后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "5";
// 填充到長度為3,使用默認(rèn)空格填充
const padded = str.padStart(3);
console.log(padded); // 輸出: "  5"

// 填充到長度為5,使用"0"填充
const zeroPadded = str.padStart(5, "0");
console.log(zeroPadded); // 輸出: "00005"

// 如果目標(biāo)長度小于原字符串長度,則返回原字符串
const shorter = str.padStart(1);
console.log(shorter); // 輸出: "5"

2. padEnd()

  • 作用:在字符串的結(jié)尾填充指定的字符,直到達(dá)到指定的長度
  • 語法str.padEnd(targetLength[, padString])
  • 參數(shù)
    • targetLength - 必需,數(shù)字,目標(biāo)字符串的長度
    • padString - 可選,用于填充的字符串,默認(rèn)為空格
  • 返回值:字符串,填充后的新字符串
  • 是否改變原字符串:否
  • 示例
const str = "5";
// 填充到長度為3,使用默認(rèn)空格填充
const padded = str.padEnd(3);
console.log(padded); // 輸出: "5  "

// 填充到長度為5,使用"0"填充
const zeroPadded = str.padEnd(5, "0");
console.log(zeroPadded); // 輸出: "50000"

// 使用多個(gè)字符進(jìn)行填充
const multiChar = "Hi".padEnd(7, "abc");
console.log(multiChar); // 輸出: "Hiabcab"(循環(huán)使用填充字符串)

十、其他字符串方法

1. match()

  • 作用:在字符串中查找匹配正則表達(dá)式的結(jié)果
  • 語法str.match(regexp)
  • 參數(shù)regexp - 必需,正則表達(dá)式對象
  • 返回值:數(shù)組,包含匹配結(jié)果;如果沒有找到匹配,則返回null
  • 是否改變原字符串:否
  • 示例
const str = "The quick brown fox jumps over the lazy dog";

// 查找所有以字母"o"開頭的單詞
const matches = str.match(/o\w+/g);
console.log(matches); // 輸出: ["ox", "over", "og"]

// 查找第一個(gè)匹配項(xiàng)的詳細(xì)信息
const firstMatch = str.match(/q\w+/);
console.log(firstMatch); 
// 輸出: ["quick", index: 4, input: "The quick brown fox jumps over the lazy dog", groups: undefined]

// 沒有找到匹配項(xiàng)
const noMatch = str.match(/z\w+/);
console.log(noMatch); // 輸出: null

2. matchAll()

  • 作用:返回一個(gè)包含所有匹配正則表達(dá)式的結(jié)果及分組捕獲的迭代器
  • 語法str.matchAll(regexp)
  • 參數(shù)regexp - 必需,正則表達(dá)式對象(必須包含全局標(biāo)志g)
  • 返回值:迭代器,包含所有匹配結(jié)果
  • 是否改變原字符串:否
  • 示例
const str = "Hello 123, Hello 456";
const regex = /Hello (\d+)/g;

// 獲取所有匹配結(jié)果的迭代器
const matches = str.matchAll(regex);

// 將迭代器轉(zhuǎn)換為數(shù)組
const results = Array.from(matches);
console.log(results);
// 輸出: [
//   ["Hello 123", "123", index: 0, input: "Hello 123, Hello 456", groups: undefined],
//   ["Hello 456", "456", index: 11, input: "Hello 123, Hello 456", groups: undefined]
// ]

3. search()

  • 作用:查找與正則表達(dá)式相匹配的子字符串的位置
  • 語法str.search(regexp)
  • 參數(shù)regexp - 必需,正則表達(dá)式對象
  • 返回值:數(shù)字,第一個(gè)匹配項(xiàng)的索引;如果沒有找到匹配,則返回-1
  • 是否改變原字符串:否
  • 示例
const str = "Hello World";
// 查找第一個(gè)大寫字母的位置
const upperPos = str.search(/[A-Z]/);
console.log(upperPos); // 輸出: 0

// 查找"World"的位置
const worldPos = str.search(/World/);
console.log(worldPos); // 輸出: 6

// 查找不存在的模式
const noPos = str.search(/Java/);
console.log(noPos); // 輸出: -1

4. localeCompare()

  • 作用:比較兩個(gè)字符串,考慮當(dāng)前區(qū)域設(shè)置
  • 語法str.localeCompare(compareString[, locales[, options]])
  • 參數(shù)
    • compareString - 必需,要比較的字符串
    • locales - 可選,指定區(qū)域設(shè)置
    • options - 可選,配置比較選項(xiàng)
  • 返回值:數(shù)字,表示比較結(jié)果(-1:當(dāng)前字符串在前;0:相等;1:當(dāng)前字符串在后)
  • 是否改變原字符串:否
  • 示例
const str1 = "apple";
const str2 = "banana";

// 比較兩個(gè)字符串
console.log(str1.localeCompare(str2)); // 輸出: -1("apple" 在 "banana" 之前)
console.log(str2.localeCompare(str1)); // 輸出: 1("banana" 在 "apple" 之后)
console.log(str1.localeCompare("apple")); // 輸出: 0(相等)

// 考慮地區(qū)的比較(德語中 "?" 被視為 "a" 的變音)
console.log("?".localeCompare("z", "de")); // 輸出: -1
console.log("?".localeCompare("z", "sv")); // 輸出: 1(在瑞典語中 "?" 排在 "z" 之后)

總結(jié) 

到此這篇關(guān)于JavaScript字符串常用屬性方法匯總及詳解的文章就介紹到這了,更多相關(guān)JS字符串常用屬性方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

最新評論