FUNCTIONS OF STRING class
Method Description Return Example
type
length() Finds the length of a string. Counts the number of int String str= ”COMPUTER IS FUN”;
characters in a string including blank space Int l=str.length();
OUPUT: 15
charAt(x) Extracts a single character from the specified index char String str=”COMPUTER”;
(x) of a string char ch=str.charAt(4);
OUTPUT: U
substring(x) Extracts a part of the string from the specified String String str=”COMPUTER”;
index (x) till the end of the string String nstr=str.substring(3);
OUTPUT: PUTER
substring(x,y) Extracts a part of the string from the specified start String String str=”COMPUTER”;
index (x) upto the specified end index (y) String nstr=str.substring(1.5);
OUTPUT: OMPU
indexOf(x) Finds the index of the first occurrence of the int String str=”MISSISSIPPI”;
specified character (x) in a string int p=str.indexOf(‘I’);
OUTPUT: 1
lastIndexOf(x) Finds the index of the last occurrence of the int String str=”MISSISSIPPI”;
specified character (x) in a string int p=str.indexOf(‘I’);
OUTPUT: 10
equals(x) Checks whether two strings are equal or not boolean String str1=”HI”, str2=”Hi”;
It is case-sensitive – treats upper case and lower- boolean b=str1.equals(str2);
case characters differently OUTPUT: false
equalsIgnoreCase(x) Checks whether two strings are equal or not boolean String str1=”HI”, str2=”Hi”;
It is not case-sensitive boolean
b=str1.equals(IgnoreCase(str2);
OUTPUT: true
startsWith(x) Checks whether a string begins with a specified boolean String str1=”Unlikely”;
prefix or not boolean b=str1.startsWith(“Un”);
OUTPUT: true
endsWith(x) Checks whether a string ends with a specified boolean String str1=”Unlikely”;
suffix or not boolean b=str1.endsWith(“ly”);
OUTPUT: true
trim() Removes any blank space before or after a string String Sring str=” ABC “;
String nstr=str.trim();
OUTPUT: ABC
replace(x,y) Replaces the specified old character (string) with a String String str=”ABRACADABRA”;
specified new character (string) in a string String nstr=str.replace(‘A’,’Z’);
String str=”old is gold”; OUTPUT: ZBRZCZDZBRZ
String nstr=str.replace(“old”,”new”);
OUTPUT: new is gold
toUpperCase() Converts all the lower-case characters in a string to String String str=””Abc”;
upper-case String nstr=str.toUpperCase();
OUTPUT: ABC
toLowerCase() Converts all the upper-case characters in a string String String str=””Abc”;
to lower-case String nstr=str.toLowerCase();
OUTPUT: abc
concat(x) Joins or combines two strings into a new string String String str1=”Lady”,str2=”Finger”;
String nstr=str.concat(str2);
OUTPUT: LadyFinger
compareTo(x) Compares two strings lexicographically. Checks int String str1=”AMEET”, str2=”AMIT”;
whether two strings are equal or greater or smaller int c=str1.compareTo(str2);
by comparing their ASCII value OUTPUT: -4
If the difference is 0 then they are equal
If the difference is a negative integer, then the first A (65) – A (65) = 0
string is smaller than the second string M (77) – M (77) = 0
If the difference is a positive integer, then the first E (69) – I (73) = -4
string is greater than the second string
FUNCTIONS OF CHARACTER wrapper class
Method Description Return Example
type
isLetter(ch) Checks whether the specified character is a letter boolean char ch=’A’;
(alphabet) or not Character.isLetter(ch);
OUPUT: true
isDigit(ch) Checks whether the specified character is a digit or boolean char ch=’9’;
not Character.isDigit(ch);
OUPUT: true
isLetterOrDigit(ch) Checks whether the specified character is an boolean char ch=’#’;
alphabet or a digit or not Character.isLetterOrDigit(ch);
OUPUT: false
isWhiteSpace(ch) Checks whether the specified character is a blank boolean char ch=’ ‘;
space or not Character.isWhiteSpace(ch);
OUPUT: true
isUpperCase(ch) Checks whether the specified character is in upper boolean char ch=’A‘;
case or not Character.isUpperCase(ch);
OUPUT: true
isLowerCase(ch) Checks whether the specified character is in lower boolean char ch=’a’;
case or not Character.isLowerCase(ch);
OUTPUT: true
toUpperCase(ch) Converts the specified lower-case character into char char ch=’a’;
upper case character Character.toUpperCase(ch);
OUTPUT: A
toLowerCase(ch) Converts the specified upper-case character into char char ch=’A’;
lower-case character Character.toLowerCase(ch);
OUTPUT: a