http://www.technicalpage.net/search/label/SQL

>> indexOf(), lastIndexOf to find the position of a letter/char in a string


               
// indexOf() : to know the position of a particular letter/char or word in the given string . And it gives the first occurance of the letter/char in the string. 
              
// index starts from 0

String word = "success";
             //0123456-> "s" is index 0 , "u" is index 1 and so on.          
               
// for example         
System.out.println("The index of u is : " +word.indexOf("u"));
               
//when two letter are together, the index of first letter is returned.
System.out.println("The index of c is : " +word.indexOf("c"));         
               
String word1="success ful";

// space are considered as a character.
System.out.println("The index of f is : " +word1.indexOf("f"));
               
//indexOf() will also take char datatype
char txt = 'c';
System.out.println("The index of c when input is provided as char : " +word1.indexOf(txt ));
//OR // let's give the input directly as char as below for f :
System.out.println("The index of f when input is provided as char : " +word1.indexOf('f'));
               
// can also find out the position of string in the given string
System.out.println("The index of ces is : " +word1.indexOf("ces"));
               
// when the given letter is not present in the word, it returns -1
System.out.println("The index of a is : " +word1.indexOf("a")); // letter
System.out.println("The index of car is : " +word1.indexOf("car")); // string

// lastIndexOf() :To know the last occurance of the letter/char in the given string.
// first occurance
System.out.println("The first occurance of u is : " +word1.indexOf("u")); 

//last occurance
System.out.println("The last occurance of u is : " +word1.lastIndexOf("u")); 
               
      
Answer:
The index of u is : 1
The index of c is : 2
The index of f is : 8
The index of c when input is provided as char : 2
The index of f when input is provided as char : 8
The index of ces is : 3
The index of a is : -1
The index of car is : -1
The first occurance of u is : 1
The last occurance of u is : 9



//occurance after given text/index

String word = "This world is beautiful.";
             
System.out.println("In normal it displays 1st occurance of i as : "+word.indexOf("i"));
             
System.out.println("Occurance of i after 5th index as : "+word.indexOf("i", 5)); 

Answer:
In normal it displays 1st occurance of i as : 2

Occurance of i after 5th index as : 11


//Another way
             
String test = new String("This world is beautiful. Fish lives in Sea.");
             
System.out.println("In normal it displays 1st occurance of is as : "+test.indexOf("is"));
             
System.out.println("Occurance of is after 15th index as : "+test.indexOf("is", 15));

Answer :
In normal it displays 1st occurance of is as : 2
Occurance of is after 15th index as : 26

//OR
             
String test = new String("This world is beautiful. Fish lives in Sea.");
String word = new String("is");
             
System.out.println("In normal it displays 1st occurance of is as : "+test.indexOf(word));
             
System.out.println("Occurance of is after 15th index as : "+test.indexOf(word, 15));    

Answer :
In normal it displays 1st occurance of is as : 2

Occurance of is after 15th index as : 26



No comments:

Post a Comment