Var isContainsAfterPosition = actualstring.indexOf("In",2) != -1 var actualstring = "In ES5 use IndexOf method", The indexOf method will return the substring position if it found, otherwise returns -1.Īnd indexOf method is case sensitive,accepts two parameter similar to includes method. Var isContains=actualstring.indexOf(substringToCheck) != -1 Go through the following code example to understand it further. In ES5 and before versions of Javascript we can use indexOf method to check if a string contains a substring or not. In older versions of Javascript, we can add a polyfill for include method if (!) 2.In ES5 and before using indexOf method # Includes method is very easy to use and comes with additional options like search position. As we are checking for ES6 substring after position 1. Var isContains = actualstring.includes(substringtocheck,1) If you want to check whether a string contains a substring only after a certain position you can pass the search position as a parameter as shown in the below examples. The second parameter search position is optional and the default value is 0. Javascript string includes method accepts two parameters Includes method is case sensitive, so the below substring check returns false var substringtocheck = "Includes" Var isContains = actualstring.includes(substringtocheck) Var actualstring="ES6 contains includes method" We will take an example and understand it further. If you are using latest versions of Javascript like ES6 you can use include method to check if a string contains a substring or not as shown below. 9.Best way to check if a string contains a substring or not in javascript:.6.Javascript String Contains Case-insensitive check.2.In ES5 and before using indexOf method.
0 Comments
Leave a Reply. |