what does mean indexOf("searchString") == -1 this only for string or by alphabet also

Rajababu
Giga Guru

Hi ,

Can any one explain above term .

if indexOf("Gaurav") == -1 to indexOf("Gaurav") >-1 what it says

Line Gaurav is working in Servicenow or Servicenow working for gaurav or Every one knows gaurav working for Servicenow.

Can any one explain .nayan_awadhiya pradeepksharma

1 ACCEPTED SOLUTION

Gowrisankar Sat
Tera Guru

indexOf() refers to the Index where that particular string is found.



var string = testGaurav


Here: indexOf("Gaurav") returns 4.



var string = Gaurav


Here: indexOf("Gaurav") returns 0.



var string = testtest


Here: indexOf("Gaurav") returns -1, since the string is not found.



Which means, if string is found anywhere, it returns value starting from 0.


View solution in original post

11 REPLIES 11

Patrick Schult2
Giga Guru

indexOf is a method of JavaScript String that searches the string for matching text. indexOf will return -1 if a match was not found, or the number of the index where the match starts.



So indexOf("Gaurav") == -1 is saying "If Gaurav is not found in the string, then..."



Read more about how to use indexOf here.


Midhun1
Giga Guru

Hi Gaurav,



Check this for more info:


JavaScript String indexOf() Method


nthumma
Giga Guru

This method returns -1 if the value to search for never occurs.



for example


var str= 'Line Gaurav is working in Servicenow';



str.indexOf("Gaurav") >-1 returns true , because string has 'Gaurav'.



str.indexOf("Pooja") >-1 returns false , because string does not have 'Pooja'.








ANINDYA SUNDAR
Tera Contributor

let's see

 

indexOf(" data") here we will search the sub-string ("data" ) is present on the main string or not ?

if it has present then i will return 1 other wise it returns -1 .

 

we know ture =1 & false = 0    ==> so why here the false condition returns -1 ? 

 

because 0 is the first possition of a string ..  so it may occure an ambiguidy .

lcbell
Tera Expert

How to test to see if one string contains another string of the same case?

var text = 'Hello there';
var textToFind = 'Hello';

if( text.indexOf( textToFind ) == -1 ) // true if text does not contain textToFind

The following statements have the same result

if( text.indexOf( textToFind ) > -1 ) // true if text contains textToFind
if( text.indexOf( textToFind ) != -1 ) // true if text contains textToFind
if( text.indexOf( textToFind ) >= 0 ) // true if text contains textToFind

Instead of using one of three different comparisons to check the result we can use a simple line of code below to handle it, which has fewer operations than the comparison.

If the text starts with the textToFind the result of text.indexOf( textToFind ) is zero (0).
0 is false but we want to convert it to true since the textToFind was found

If the text does not contain textToFind the result of text.indexOf( textToFind ) is negative one (-1).
All non zero numbers are true but we want -1 (not found) to be converted to false

One solution to convert the result and make it more simple is to use the ~ operator
The bitwise NOT operator, the tilde ~, will invert the bits of its operand.
var x = text.indexOf( textToFind );
The formula for ~x is -x − 1, change the sign of x then subtract 1
~(-1) === 0, which evaluates to false
for any other result (not -1) it evaluates to true.
~x where x != -1 evaluates to true, e.g., ~0 === -1, ~1 === -2, ~(-2) === 1

if( ~text.indexOf( textToFind ) ) // result is true if match is found.

So if you want to test to see if one string does not contain another string of the same case, then you just put a ! (logical NOT) in front of it.
if( !~text.indexOf( textToFind ) ) // true if text does not contain textToFind.