Check whether a string contains another string and concatenate javascript

You can use String#indexOf().

Based on the docs, .indexOf() for Strings accepts a search value parameter. If the search value is found, it returns a non-negative value. If it does not find your search value, it returns -1.

let word = 'Hello world!';
let query = 'world';
let anotherQuery = 'value';

word.indexOf(query); // returns 6 - which is a non-negative value
word.indexOf(anotherQuery); // returns -1

Which you can use it this way.

let isExist = word.indexOf(query) !== -1;

Or you can write a function for it

function find(haystack, needle) {
  let isFound = haystack.indexOf(needle);

  return isFound !== -1;
}

let isExist = find(word, query);

This post will discuss how to check whether a string contains another string as its substring in JavaScript.

1. Using String.prototype.includes() function

The includes() method can be used to check whether a given string includes another string or not. It returns true if the search string is found within the given string; false otherwise. Here’s how the code would look like:

varsource='Hello World';

vartarget ='llo';

console.log(source.includes(target));

Download  Run Code

 
This method has been introduced with the ES6 and may not be available in all JavaScript implementations yet. However, MDN has a simple polyfill for this method:

if(!String.prototype.includes){

    String.prototype.includes= function(search,start){

    'use strict';

    if(search instanceofRegExp){

        throwTypeError('first argument must not be a RegExp');

    }

    if(start===undefined){ start=0;}

    return this.indexOf(search,start)!== -1;

    };

}

2. Using Lodash Library

If you are using lodash library, the _.includes method will be useful:

var_=require("lodash");

varsource='Hello World';

vartarget ='llo';

console.log(_.includes(source, target));

Download Code

3. Using String.prototype.indexOf() function

The indexOf() method returns the index of the first occurrence of the target or -1 when the target string is not found. The following example demonstrates its usage.

varsource='Hello World';

vartarget ='llo';

console.log(source.indexOf(target)!== -1);

Download  Run Code

4. Using String.prototype.search() function

Finally, you can also use the search() method to check if the source string contains the target string. The following example would return the index of the first match of the target in the given string, or -1 if it found no match.

varsource='Hello World';

vartarget ='llo';

console.log(source.search(target)!== -1);

Download  Run Code

That’s all about determining whether a string contains another string as a substring in JavaScript.

How do you check if a string contains another string in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check if a string contains another string?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

What does .includes do in JavaScript?

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

How do you equate two strings in JavaScript?

To compare two strings in JavaScript, use the localeCompare() method. The method returns 0 if both the strings are equal, -1 if string 1 is sorted before string 2 and 1 if string 2 is sorted before string 1.