STRING FUNCTIONS
String s = “COMPUTER”;
C O M P U T E R
0 1 2 3 4 5 6 7
String p = “Applications”;
A p p l i c a t i o n s
0 1 2 3 4 5 6 7 8 9 10 11
Sl No Function Description Data Type Example Output
1 It returns the number of characters of a int [Link]( ) 8
length( ) given string
[Link]( ) 12
2 It returns a character from a given index char [Link](3) P
charAt( )
[Link](10) n
3 It returns the index of a given character int [Link](‘M’) 2
indexOf( )
[Link](‘i’) 4
4 lastIndexOf( ) It returns the last index of a given int [Link](‘i’) 8
character
5 toLowerCase( ) It returns the string in lower case letters String [Link]( ) computer
6 toUpperCase( ) It returns the string in upper case letters String [Link]( ) APPLICATIONS
7 trim ( ) It removes the spaces from beginning and String “ Java ”.trim( ) Java
end of a string
8 It joins two strings String [Link](p) or s+p COMPUTERApplications
concat( ) (The + operator can also be used to join
[Link](s) or p+s ApplicationsCOMPUTER
strings)
9 It checks if a string begins with a given boolean [Link](“COMP”) true
startsWith( ) string or not
[Link](“App”) true
10 It checks if a string ends with a given string boolean [Link](“COMP”) false
endsWith( ) or not
[Link](“cations”) true
11 It replaces one character of a string with String [Link](‘O’, ‘#’) C#MPUTER
replace( ) another or one substring by another
[Link](‘a’, ‘@’) Applic@tions
[Link](“UTER”, “ANY”) COMPANY
12 It checks if two strings are equal or not and boolean [Link](p) false
equals( ) returns true or false
“JAVA”.equals(“java”) false
equalsIgnoreCase( ) It checks if two strings are equal or not boolean “JAVA”.equalsIgnoreCase true
13 ignoring the case (“java”)
14 It compares the ASCII codes of two strings [Link](p) 2 [C - A = 67 - 65]
and returns the difference of the ASCII code int
[Link](s) ̶ 2 [A – C = 65 – 67]
of the different character
compareTo( ) “bat”.compareTo(“ball”) 8 [t – l = 116-108]
“java”.compareTo(“java”) 0
[Link](“COM”) 5
“COMPUTE”.compareTo(s) ̶ 1 (7-8=-1)
15 It compares the ASCII codes and returns int “JAVA”. compareTo 0
compareToIgnore the difference ignoring the case
IgnoreCase (“java”)
Case( )
“bluej”.compareTo -8
IgnoreCase(“JAVA”)
16 It returns a part of the String based on String [Link](5) cations
index
[Link](0) COMPUTER
substring( ) [Link](0,4) COMP
[Link](5,8) cat
[Link](5,10) Runtime error
(StringIndexOutOfBounds)