Searching characters and String in Java

1. Searching a string

String class provides indexOf ( ) method which searches for the specified character inside the string object. This method has been overloaded. If the search is successful, then it returns the index of the char otherwise -1 is  returned. 

1. int indexOf(String str) : 

Finds the start index of the first occurrence of the substring argument in a String.


int position= str1.indexOf (“e”); // for ”hello” it returns 1 

2. int indexOf(String str, int fromIndex)  

Finds the start index of the first occurrence of the substring argument in a String,  starting at the index specified in the second argument. 


Ex: int position= str1.indexOf (“e”,3); // for ”hello” it returns -1 

3. int indexOf(char c): 

Returns the index of first occurrence of the argument char .


 int position= str1 .indexOf( e ); // for welcome it returns 1 4)
 int indexOf(int c, int fromIndex). // Returns the index of first occurrence of the  argument char.  
 int position= str1.indexOf(e , 3 ); // for welcome it returns 6  

The String class also provides methods to search for a character or string in backward  direction.  

 These methods are given below.  
 1. int lastIndexOf(int ch)  
 2. int lastIndexOf(int ch, int fromIndex)  
 3. int lastIndexOf(String str)  
 4. int lastIndexOf(String str, int fromIndex)   

2. Replacing a character in string.

The replace method of String class can be used to replace all occurrences of the  specified character with given character.  


str. replace (char oldChar, int newchar)  
String str1=“Hello”; 
str1.replace (“H” ,”c”); // Gives string “cello” instead of “Hello”  

3. Find substring in java

String class provides substring ( ) method to extract a specified portion of the given  String. This method has been overloaded.  If the index value is not valid, a StringIndexOutOfBoundsException is thrown.

Note:  A new String object containing the substring is created and returned. The original  String won’t be affected.  

1) String substring (int startIndex)  – gives substring starting from nth character 

2) String substring (int startIndex, int endIndex)  – gives substring starting from nth character up to mth character (not including mth )

4. String conversions.

String class provides set of static overloaded valueOf ( ) method to convert primitives types and object into strings. 

Conversion of String from Object to string: 

 • static String .valueOf(Object obj)  
 • static String. valueOf(char[] character)   

Conversion of String from Primitive to string: 

 • static String .valueOf(boolean b)  
 • static String .valueOf(char c)  
 • static String .valueOf(int i)  
 • static String. valueOf(long l)  
 • static String .valueOf(float f)  
 • static String .valueOf(double d)   

5. String Manipulators.

String class provides following methods to manipulate character case in String. 

1. toUpperCase ( ) : Converts the given string into uppercase letter.


 String str1=”Hello rutu”; 
 str2=str1.toUpperCase; // string in str2 will be HELLO  

 2. toLowerCase ( ) : Converts the given string into lowercase letter.


 String str1=”HELLO RUTU”; 
 str2=str1.toLowerCase; // string in str2 will be hello rutu

Note: Original String object is returned if none of the characters changed; otherwise new  String object is constructed and returned.  

 3. trim( ) : This method removes white space from the front and the end of a String


 String str=" hello.. "; 
 System.out.println(str.trim()); // output will be hello.., no space before  and after as original string str 

 4. int length( ) : Returns length of the String.  


 Ex: String str="hello"; 
 System.out.println(str.length ( ));  

 5. charAt( ): Gives nth character of string. 


 Ex: String str="hello"; 

6. p.toString( ): Creates a string representation of the object p. 


str1.concat (str2); //output will be “HelloWorld 

Rutu Shah