Home >Web Front-end >JS Tutorial >How to use startwith function

How to use startwith function

不言
不言Original
2019-02-18 16:03:0124197browse

The startwith function is used to check whether the given string starts with the characters of the specified string. Its usage syntax is "str.startsWith(searchString, position)", and the parameter searchString represents the string.

How to use startwith function

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.

The str.startsWith() function in JavaScript is used to check whether the given string starts with the characters of the specified string. Let's take a look at the specific usage of the startsWith() function.

Let’s first take a look at the basic syntax of the startsWith() function

str.startsWith(searchString , position)

The first parameter searchString of the startsWith() function represents a string, which will be given Search at the end of the string. The second parameter of the function is position, which determines the position within the given string where searchString is searched.

This function returns a Boolean value true if the searchString is found, otherwise it returns a Boolean value false.

Let’s look at the specific example

The code is as follows

Check whether the string str starts with It

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script> 
function func() { 
    var str = &#39;It is a great day.&#39;; 
    var value = str.startsWith(&#39;It&#39;);  
    document.write(value); 
} 
func(); 
</script> 
</body>
</html>

Output results For: true

Check whether the string str starts with great

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script> 
function func() { 
    var str = &#39;It is a great day.&#39;; 
    var value = str.startsWith(&#39;great&#39;);  
    document.write(value); 
} 
func(); 
</script> 
</body>
</html>

The output result is: false

Check the string Whether str starts with great at the specified index 8

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script>   
function func() { 
    var str = &#39;It is a great day.&#39;; 
    var value = str.startsWith(&#39;great&#39;,8); 
    document.write(value); 
} 
func(); 
</script> 
</body>
</html>

The output result is: true

This article ends here. For more exciting content, you can pay attention to php Chinese Other related column tutorials on the Internet! ! !

The above is the detailed content of How to use startwith function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn