JavaScript Program to Check Whether a String Starts and Ends with Certain Characters

If you’ve been writing JavaScript code for a longer period, you probably encounter situations when you’ll need to figure out whether a string starts or ends with a specific character set.

For instance, suppose you are filtering out the type of file you have stored on your PC based on the file’s extension and want to confirm the legitimacy of a file’s extension. The ‘startsWith()’ and ‘endsWith()‘ methods in JavaScript can be useful in this case. In this tutorial, we will learn how to use these methods with the help of practical examples.

JavaScript startsWith

Javascript ‘startsWith()’ function lets you know if a string starts with a certain character set or not. If the characters provided as an argument are the starting characters in the string, it will return ‘true’, otherwise, it will return ‘false’.

Syntax:

string.startsWith(substring, position);
  • substring (required): The characters to search for at the start of the string.
  • position (optional): The starting point for the search within the string. It has a default value of 0.

Example 1:

Let’s take the assumption that you’re creating a web application that checks phone numbers for validity using the national code. If you want to find out what nation it is from, you can use the startsWith() function.

var phoneNumber01 = "+1 123 456 7890"; 
console.log(phoneNumber01.startsWith("+1")); 

Output:

JavaScript startsWith Example 1 Output

From the above example, we can see that the startsWith() method is used to find out if the phone numbers begin with a particular country code. It returns true as the phone number starts with the provided country code.

Example 2:

Now, suppose that you want to verify the phone number, but this time the twist is that you want to search the phone number which contains ’20’ as a digit and the search begins from the 4th index.

var phoneNumber02 = "+44 20 1234 5678"; 
console.log(phoneNumber02.startsWith("20", 4));

Output:

JavaScript startsWith Example 2 Output

In the above example, the search starts from index 4 in the string ‘phoneNumber2′, and as you can see that ’20’ is found at that position, it returns true.

JavaScript endsWith

Javascript ‘endsWith()’ function lets us determine whether a string ends with a certain character set, it returns ‘true’ if the string ends with the provided characters otherwise it returns ‘false’.

Syntax:

string.endsWith(substring, position);
  • substring (required): The characters to search for at the end of the string.
  • position (optional): The ending point for the search within the string. The default value is the length of the string.

Example 1:

Assume for the moment that you are creating a file management system and that you must verify file extensions. Before processing a file, you can use the endsWith() function to make sure the extension is legitimate.

var fileName1 = "document.pdf";
console.log(fileName1.endsWith(".pdf")); 

Output:

JavaScript endsWith Example 1 Output

From the above example, we can see that the endsWith() method is used to find out whether the file names end with an extension we have specified. It returns true in the console because the file name ends with the specified extension.

Example 2:

Let’s try to find the ‘.jpg’ extension and limit the search to the first 8 characters of the string.

var fileName2 = "image.jpg";
console.log(fileName2.endsWith(".jpg", 8));

Output:

JavaScript endsWith Example 2 Output

In the above example, we can see the use of the position parameter. The search only considers the first 8 characters of the string, which is ‘image.jp’ and checks whether it ends with the word ‘.jpg’ and since ‘image.jp’ does not end with ‘.jpg’, it returns false.

Conclusion

In this tutorial, we have learned how to use the startsWith() and endsWith() methods in JavaScript, and also we have seen where these methods can be used. These methods can be useful if you deal with a lot of data values and want to filter out them based on the start or end of a set of characters. We hope you enjoyed it.

Also Read:

Reference

https://stackoverflow.com/questions/49785364/join-string-based-on-startswith-and-endswith

Adarshita Gupta
Adarshita Gupta
Articles: 10