Find a specific word ends with a specific character in the string using JavaScript

Create a program to find a specific word that ends with a specific character in the string using Javascript. Let’s create simple web pages to design and perform the string function using HTML, CSS, JS.

Problem Statement

In this article, we have to learn how to find specific word ends with a specific character in the string for that use the javascript function that will return the specific character of the string.

Solution

  • Create HTML file
  • In that create heading as check string ends with specified characters in <h2></h2> tag
  • In the Javascript enter  the first string using const, const str1 = ‘I cant wait to go to Disneyland!’;
  • Display first string using the document.write document.write(“<b>”+”String 1 -“+str1+”</br>”+”</b>”);
  • Calculate string for check string ends with specified character use endsWith() method  if string ends with given string it will print true and endstring not matches it will print false and print output document.write(“String ends with Disneyland!-“+”</br>”+str1.endsWith(‘Disneyland!’) + “</br>”);
  • Enter second string  const str2 = ‘Today is good day’;
  • Display second string using the document.write document.write(“<b>”+”String 2 -“+str2+”</br>”+”</b>”);
  • Print output  document.write(“String ends with ‘?'”+”</br>”+str2.endsWith(‘?’));

JavaScript code to find string ends with specific characters in string

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Check string ends with</title>
</head>
<body style="text-align: center;">
    <h2 style="color: orangered;font-weight: bold;font-style: initial;">Check string ends with Specified characters</h2>
<script type="text/javascript">
    const str1 = 'I cant wait to go to Disneyland!';
    document.write("<b>"+"String 1 -"+str1+"</br>"+"</b>");
    document.write("String ends with Disneyland!-"+"</br>"+str1.endsWith('Disneyland!') + "</br>");

    const str2 = 'Today is good day';
    document.write("<b>"+"String 2-"+str2+ "</br>"+"</b>");

    document.write("String ends with '?'"+"</br>"+str2.endsWith('?'));

</script>
</body>
</html>

Output

In this way, we will learn how to find the string end with specified characters using JS.