JavaScript 字串 endsWith() 方法



JavaScript 字串 endsWith() 方法用於確定字串是否以指定的字元(子字串)結尾。如果在字串末尾找到了指定的子字串字元,則返回布林值“true”;否則,返回“false”。

如果 searchString 引數是正則表示式,則此方法會丟擲 TypeError 異常。它是區分大小寫的,因此它將“Hello”和“hello”視為不同的字串。

語法

以下是 JavaScript 字串 endsWith() 方法的語法:

endsWith(searchString, endPosition)

引數

此方法接受兩個引數:“searchString” 和“endPosition”。endPosition 引數是可選的,並指定字串中搜索結束的索引(位置)。

  • searchString − 要在字串末尾搜尋的字元。
  • endPosition(可選) − 字串中搜索結束的位置。

返回值

如果字串以指定的子字串結尾,則此方法返回 true,否則返回 false

示例 1

如果字串以指定的字元結尾,則返回 true

在此示例中,我們使用 JavaScript 字串 endsWith() 方法來檢查字串“TutorialsPoint”是否以指定的子字串“Point”結尾。

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "TutorialsPoint";
   const searchString = "Point";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", str, "' ends with '", searchString, "'or not? ", str.endsWith(searchString));
</script>
</body>
</html>

輸出

以上程式返回“true”。

Original string: TutorialsPoint
Search string: Point
Is string 'TutorialsPoint' ends with 'Point'or not? true

示例 2

如果字串沒有以指定的字元結尾,則返回 false

以下是 JavaScript 字串 endsWith() 方法的另一個示例。在此示例中,我們使用此方法來確定字串“Hello World”是否以指定的字串“Word”結尾。

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   const searchString = "Word";
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   document.write("<br>Is string '", str, "' ends with '", searchString, "' or not? ", str.endsWith(searchString));
</script>
</body>
</html>

輸出

執行以上程式後,它將返回“false”。

Original string: Hello World
Search string: Word
Is string 'Hello World' ends with 'Word' or not? false

示例 3

如果我們將 endPosition 引數值傳遞為 15,它將檢查索引 15 處的字串結尾是否以指定的子字串結尾,並且其餘字串將被忽略。如果子字串與 searchString(直到索引 15)的結尾匹配,則返回 true;否則,返回 false

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Welcome to Tutorials Point";
   let endPosition = 15;
   const searchString = "Tuto";
   document.write("Original string: ", str);
   document.write("<br>End position: ", endPosition);
   document.write("<br>Search string: ", searchString);
   document.write("<br>String actual length: ", str.length);
   document.write("<br>Is string '", str, "'(upto length 15) ends with '",searchString,"' or not? ", str.endsWith(searchString, endPosition));
</script>
</body>
</html>

輸出

執行以上程式後,它將返回“true”。

Original string: Welcome to Tutorials Point
End position: 15
Search string: Tuto
String actual length: 26
Is string 'Welcome to Tutorials Point'(upto length 15) ends with 'Tuto' or not? true

示例 4

JavaScript 字串 endsWith() 方法期望 searchString 引數為字串。如果它是正則表示式,則會丟擲“TypeError”異常。

<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   const searchString = /ab+c/;
   document.write("Original string: ", str);
   document.write("<br>Search string: ", searchString);
   try {
      document.write("Is string '",str,"' is ends with '",searchString,"' or not? ", str.endsWith(searchString));
   } catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

輸出

以上程式丟擲“TypeError”異常。

Original string: Tutorials Point
Search string: /ab+c/
TypeError: First argument to String.prototype.endsWith must not be a regular expression
廣告