Check if string ends with desired character in JavaScript


In the given problem statement we are required to check if the string ends with the desired character with the help of Javascript functionalities. So we will use basic functions and syntax of Javascript to solve the problem.

Understanding the problem

The problem at hand is to check if a given string is ending with the given specific character in Javascript. For example suppose we have a string as Hello World! and we have to check if the exclamation ‘!’ mark is present at the end of the string or not. So after comparing with the string the result is true.

This task can be done using several approaches. In this article we will discuss the logic, algorithm and sample code in Javascript. And also we will analyze the complexity for the solution and in the last conclusion.

Logic for the given problem

To determine that a string ends with the desired character in Javascript we will compare the last character of the string with the given character. If they match we will return the result as true otherwise the result will be false.

Algorithm

Step 1: As we have to check that the string is ending with the given or desired character. So for doing this task we will create a function and give it name as endsWithChar and inside this function we will pass two parameters first is string str and second is character.

Step 2: After defining the above function we will extract the last character of the string with the help of charAt method.

Step 3: Now we have the last character of the string. So we will compare the last character with the desired character with the help of ===.

Step 4: So if the result of step 3 is true then the character is present in the last of the given string. Otherwise we will return false.

Example

//Function to check that the string end with a given character
function endsWithChar(str, character) {
   const lastChar = str.charAt(str.length - 1);
   return lastChar === character;
}

// Usage example
const inputStr = "Hello, Tutorials Point!";
const inputStr1 = "Hello, World.";
const desiredChar = "!";
const endsWithDesiredChar = endsWithChar(inputStr, desiredChar);
const endsWithDesiredChar1 = endsWithChar(inputStr1, desiredChar);
console.log(endsWithDesiredChar);
console.log(endsWithDesiredChar1);

Output

true
false

Complexity

The time complexity for checking that the desired character is present at the end of the string is O(1), because the code involves extracting the last character of the string and performing simple operations. So besides the length of the string the time required to perform these operations is constant.

Conclusion

As we have performed a simple operation to check whether the string contains the desired character at the end or not. So we have used charAt and length methods of Javascript to perform the task. In this article we have discussed logic behind the operation, provided an algorithm to achieve it and time complexity of the code. So with this solution we can easily find out that the string ends with a specific character in Javascript.

Updated on: 11-Aug-2023

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements