English

Learn JavaScript – How To Check if One String Contains Another Substring?

JavaScript is one of the easiest programming languages to learn. If you are confused on how to check if one string contains another substring in JavaScript, then you have come to the right place. There are many ways one can achieve the desired result, so let’s go through them one by one.

The most common method: string.indexOf()

string.indexOf()

The best way to find a substring from a string is to use the indexOf() function. If you noticed, the indexOf() method simply tests if the substring is present or not. If it is present, it will return the starting index of the substring; if not, it will return -1. We can use the behavior of the indexOf() method to find a substring. All we need to do is set a condition to check for the return value.

var programminglanguages = "C++, JavaScript, Ruby";

var newlanguage = "Python";


function findNewProgrammingLanguage(language) {

 if (programminglanguages.indexOf(language) >=0) {

   console.log(language + "is present");

 }

 else {

   console.log(language + "is absent");

 }

}


findNewProgrammingLanguage(newlanguage);

RegExp.test() method

Next, we can use the RegExp.test() method to find a substring. This method returns a boolean and hence is much easier to use. In comparison to the indexOf() method, it returns direct true or false results and can therefore be a good alternative.

var programminglanguages = "C++, JavaScript, Ruby";

var newlanguage = "Python";

function findNewProgrammingLanguage(language) {

 var programmingReg = new RegExp(newlanguage);

 if (programmingReg.test(programminglanguages)) {

   console.log(language + "is present");

 }

 else {

   console.log(language + "is absent");

 }

}


findNewProgrammingLanguage(newlanguage);

While RegExp.test() is a great method to use, special characters can be a problem.

String.contains()

The last method is String.contains(). It directly checks if the substring is present or not and returns a boolean value. It is available after ECMAScript 6, and hence needs to be used with caution.

Do you have anything to add to the tutorial? If yes, don’t forget to comment below and let us know.

You can also check on our website for videos about JavaScript. Below are some examples:

  • Functional JavaScript (part 10) – JavaScript
  • JSbroadcast.com – Hacking in MEAN Stack (part 32) – JavaScript

You can also follow some of our broadcasters who program in JavaScript as below:

 TGOlson

 agjs

Another cool way to find out interesting things about JavaScript is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

  • Understanding string manipulation is important if you want to improve as a programmer. This tutorial explains simple stuff such as finding a string with another. Hope to know more stuff like this as it will sharpen by skill as a programmer! Oh yes, I love JavaScript!

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago