Softech Tutorial Home
Study Material for java
Only for private circulation
String Functions
All string functions are defined in [Link] package under String class. It is a default package of java
1. length( ) – It is used to calculate number of characters of a string. Return type is int. This function
takes no argument. Example :
int l=”Softech”.length();
[Link](l); output : 7
2. charAt(int) – This function is used to extract single character from any index position of a string. It
returns a character and takes one integer type parameter or argument. Example :
char ch=”Softech”.charAt(3);
[Link](ch); output : t
3. substring(int, int) – This string function is used to extract any number of characters from any
position of a string. It returns a string and takes one or two int type arguments.
Example 1 : String s=”Softech”.substring(3);
[Link](s); output: tech
Example 2 : String t=”Softech”.substring(2,6);
[Link](t); output: ftec
4. indexOf(char) – This function returns position of a character(first occurrence) in a string. It takes one
character parameter and returns an integer value.
Example 1 : int a=”Softech”.indexOf(‘f’);
[Link](a); output: 2
Example 2 : int b=”Softech tutorial”.indexOf(‘t’,4);
[Link](b); output : 8 (1st occurrence from 4 position)
5. equals(String) – It is used to check equality of 2 strings and returns true or false value.
Example : boolean x=”softech”.equals(“SOFTECH”);
[Link](x); output : false
Softech Tutorial Home
6. equalsIgnoreCase(String) – This string function also checks for equality of 2 strings but does not
consider UpperCase or LowerCase i.e. does not make difference between capital or small letter
alphabets.
Example : boolean x= “Softech”.equalsIgnoreCase(“SOFTECH”);
[Link](x); output: true
7. concat(String,String) – It concatenates or adds 2 string and returns a new string.
Example : String str=”Good”.concat(“Morning”);
[Link](str); output: GoodMorning
8. lastIndexOf(char) – This function returns the position of a character(last occurrence) in a string.
Example: int p=”Softech tutorial”.lastIndexOf(‘o’);
[Link](p); output: 12
9. trim( ) – This string function removes leading and trailing spaces (space at the end) of a string.
Example : String s=” softech tutorial “;
[Link]([Link]()); output : softech tutorial
10. compareTo(String,String) – This function checks for equality of 2 strings lexicographically and
returns positive or negative or zero
Example 1 : int p=”softech”.compareTo(“SOFTECH”);
[Link](p); output : 32
Example 2: int q=”SOFTECH”.compareTo(“SOFTECH”);
[Link](q); output: 0 (since both strings are same)
Example 3: int r=”softech”.compareTo(“infotech”);
[Link]( r ); output: 10 (ascii code of ‘s’ is higher than ‘i’)
Example 4: int s=”Softech”.compareTo(“Soft”);
[Link](s); output: 3
11. replace(char,char) – This function is used to replace all occurrences of a character by another
character in a string. It returns s String type value. Remember this function is not recommended to use
in your program
Created by Sanjay Sir Page 2
Softech Tutorial Home
Example: String s=”Malayalam”.replace(‘l’,’p’);
[Link](s); output: Mapayapam
12. toUpperCase() – This string function converts the string from mixed case or from lowercase
alphabets to uppercase alphabets and returns a new string value.
Example: String s=”Softech”.toUpperCase( );
[Link](s); output: SOFTECH
13. toLowerCase( ) - This string function converts the string from mixed case or from uppercase
alphabets to lowercase alphabets and returns a new string value.
Example: String s=”SOFTEch”.toLowerCase( );
[Link](s); output: softech
14. startsWith(String) – It checks whether a string starts with another string or not and returns a boolean
value.
Example: boolean a=”Softech”.startsWith(“Soft”);
[Link](a); output: true
15. endsWith(String) – It checks whether a string ends with another string or not and returns a boolean
value.
Example: boolean a=”Softech”.endsWith(“Soft”);
[Link](a); output: false
Differentiate between the following functions :
a) charAt() and indexOf()
charAt() indexOf()
1. It returns single character from any position 1. It returns position of a character passed as
of a string parameter.
2. Return type is char 2. Return type is int
Created by Sanjay Sir Page 3
Softech Tutorial Home
b) equals() and compareTo()
equals() compareTo()
1. It checks for equality of 2 strings directly 1. It checks 2 strings lexicographically.
2. Return type is boolean. 2. Return type is int
c) startsWith() and endsWith()
startsWith( ) endsWith( )
1. Checks whether a string starts with another 1. It checks whether a string ends with another
string or not, and returns a boolean value. string or not, and returns a boolean value.
d) length() and length
Length( ) Length
1. It is a java string function defined under 1. It is not a function, it is a java predefined
String class variable.
2. It calculates the number of characters of a 2. It is used to know the size of an array.
String.
Character class functions
All the below-mentioned functions are defined under Character class of [Link] package. These
functions deal with single character only.
1. toUpperCase(char) –It is used to convert a character to capital letter. Return type is char.
Example : char c=’d’;
char p=[Link]( c );
[Link](p); output : D
2. toLowerCase(char) - It is used to convert a character to small letter. Return type is char.
Example : char c=’D’;
char p=[Link]( c );
[Link](p); output : d
Created by Sanjay Sir Page 4
Softech Tutorial Home
3. isLetter(char) – This function is used to check whether a character is an alphabet or not. If so then
returns true, otherwise returns false.
Example : char c=’6’;
boolean a=[Link](c);
[Link](a); output : false
4. isDigit(char) - This function is used to check whether a character is a digit or not. If so then
returns true, otherwise returns false.
Example : char c=’6’;
boolean a=[Link](c);
[Link](a); output : true
5. isLetterOrDigit(char) - This function is used to check whether a character is an alphabet or a
digit. If so then returns true, otherwise returns false.
Example : char c=’6’;
boolean a=[Link](c);
[Link](a); output : true
6. isUpperCase(char) - It is used to check whether a character is capital letter or not. If so then
returns true, otherwise returns false.
Example : char c=’A’;
boolean a=[Link](c);
[Link](a); output : true
7. isLowerCase(char) - It is used to check whether a character is small letter or not. If so then
returns true, otherwise returns false.
Example : char c=’A’;
boolean a=[Link](c);
[Link](a); output : false
Students are advised that they can use the above-mentioned functions in their programs in ICSE
Created by Sanjay Sir Page 5