String Function
1. length ( ) – used to find length of any string . Its return type is integer . It counts space
also .
eg String s = “ carmel school “;
int l = s.length( );
output l =13.
2. charAt ( ) – used to extract a single character from a specified position . Its type is
character.
eg String s = “ carmel” ;
char c = s.charAt(3); output - m
char c = s.charAt(6); no output , run time error
3. substring ( ) – used to extract a group of characters . Its return type is String
eg String s = “ carmel” ;
String c = s.substring(0,3); output - car
String c = s.substring(4); output – el
String c = s.substring(4,8); no output , run time error
4. equals( ) – used to compare two string . Its return type is Boolean .It is case sensitive.
eg String s1 = “car” , s2 = “car” , s3=”CAR” ,s4 = “bus”;
boolean b1= s1.equals(s2) ; output – true
boolean b1= s1.equals(s3) ; output - false
boolean b1= s1.equals(s4) ; output – false
5. equalsIgnoreCase( ) – used to compare two string only. Its return type is Boolean .It is
not case sensitive.
eg String s1 = “car” , s2 = “car” , s3=”CAR” ,s4 = “bus”;
boolean b1= s1.equalIgnoreCase(s2) ; output – true
boolean b1= s1.equalsIgnoreCase(s3) ; output - true
boolean b1= s1.equalsIgnoreCase(s4) ; output – false
6. toUpperCase( ) – used to convert letters of the string in upper case. Its type is
string.Numbers will be unchanged.
String s1= “carmel” , s2=”alto k10” ; char c = ‘d’;
String s= s1.toUpperCase( ) ;output – CARMEL
String s= s2.toUpperCase( ) ;output – ALTO K10
7. toLowerCase( ) – used to convert letters of the string in lower case. Its type is string.
Numbers will be unchanged.
String s1= “CARMEL” , s2=”ALTO K10” ; char c = ‘D’
String s= s1.toLowerCase( ) ; output – carmel
String s= s2.toLowerCase( ) ; output – alto k10
8. indexOf( ) - used to return the position or index of first occurrence of a character in a
given string from beginning .Its type is integer . It returns -1 if character is not
available in a string. It is case sensitive.
String s = “ABRA KA DABRA” ;
int p = s.indexOf(‘B’) ouput – 1
int p = s.indexOf(‘B’,2) ouput - 10
int p = s.indexOf(‘C’) ouput - -1
9. lastIndexOf( ) - used to return the position or index of first occurrence of a character
in a given string from LAST .Its type is integer . It returns -1 if character is not
available in a string. It is case sensitive.
String s = “ABRA KA DABRA” ;
int p = s.lastIndexOf(‘B’) ouput – 10
int p = s.lastIndexOf(‘B’,8) ouput - 1
int p = s.lastIndexOf(‘C’) ouput - -1
10. replace ( ) – used to replace a character by another character or a string by another
string at all its occurrences in a given string. It is case sensitive .
String s = “The green bottle is in green bag” , s1 = “MAMA”
String p =s.replace (“green” ,”red” ) ; output - The red bottle is in red
bag
String p1=s.replace(‘M’ ,’P’); output – PAPA
11. concat( ) – used to join two string .
String s1 = “carmel” ,s2 = “school” ;
String s=s1.concat(s2); output – carmelschool
12. trim ( ) –used to remove leading and trailing blank space from the string .Others blank
space in between the words will remain unchanged.
String s= “ carmel school “;
String p = s. trim( ) ; output – carmel school
13. endsWith ( ) – used to check whether a given string ends with specified string or not.
It is of Boolean type .
String s = “ ABRA KA DABRA”
boolean b= s.endsWith( “DABRA”); output – true
boolean b1= s.endsWith( “KABRA”); output - false
boolean b2= s.endsWith( “ABRA”); output – true
14. startsWith ( ) – used to check whether a given string starts with specified string or
not. It is of Boolean type .
String s = “ ABRA KA DABRA”
boolean b= s.startsWith( “DABRA”); output – false
boolean b1= s.startsWith( “ABRA”); output - true
boolean b2= s.startsWith( “A”); output – true