Predefined functions of the String class
String : is a predefincd class in java and the objcct of string contains collcction of
characters without any white space encloscd within double invertcd quoies
ended with a null character
Length( ) Calculatcs the length of the string and returns an intcger value
Bg String x"Apple Banana"
Int1x.lcngth()
System.out.println(l );
Output: 12
Note : The counting of the character starts from
charAt( ) Uscd to Cxtract a character from the spccificd position
Eg String x=" Apple"
char z=x.charAt(3)
System.out.print'n(z);
Output : 1
Note: The counting of the character starts from 1 but the characters
are storod from position 0 in the string similar to array in
which index value starts from 0
Replaccs cvery occurrencc of the old character with a ncw character
replace(
Syntax :string. replacc( old character,new character)
Eg String x=" Apple "
System.out.println(x.replace('p','x)};
Output : Axxle
cquals( ) Checks whether two strings are exactly the same and returns a
boolcan value
Eg String a="mom", b-"Mom";
boolean x- a.equals(b);
System.out.printin(x);
Output: false
equalslgnore Case() Checks whether two strings are exactly the sarne ignoring the case
of the string s and returns a boolcan value true or falsc.
Eg String a="mom", b "Mom":
boolcan x a.cqualslgnorcCasc(b);
System.out.println(x);
Output: Truc
toUpperCase() Converts the entirc string to upper case
Eg String a Computer"
a.toUppcrCasc();
System.out,printin(a);
Output : COMPUTER
toLowerCase() Converts the entire string to lower case
|Eg String a " Computcr"
a.tol.owcrCasc);
System.out.println(a);
Output :computer
trim( Removes whitespacc/blank spaces ifany in the beginning and at
the cnd in the string
substring0 Crcates a ncw string which is the substring of the original
Eg String a-"Computer", b; Eg String a-"Computer", b;
a.substring(2,5); b= a.substring(4);
System.out. printin(b); System.out.println(b);
Output: mpu Output: uter
Comparc two strings lexographically (ie according to the
dictionary) and returns an integer valuc as an output.
compareTo() Rcturns valuc 0 ifboth the strings arc thc samc
Bg String a "apple",b "upple":
int x a.comparcT'o(b):
System.oul.println(x);
Output : 0
Returns valuc greater than 1 ifthe sccond string comparcd with the
first string is in alphabetical order after the first
ig String a "applc", b "canberry";
int x b.compare'T'o(u);
Systcm.oul.println(x);
Output : 2
Returns value lesser than 1 if the first string comparcd with the
ScCond is in order before the sccond.
Eg String a "applc"alpnabeeanberry";
int x a.comparcTo(b);
System.out.println(x);
Output : -2
Same function as that of comparc'To mcthod but ignores the casc of
compareTolgnoreCasc( )
the strings while comparing.
toString() Converts other primitive date type to a string objcct.
Eg int a= 10;
String x= a.toString();
System.out.println(x);
Output 10 (In the Memory the value of x will be "10")
indexOf() Returns the position of first occurrence of the specified character in
the string
|Eg String s-"apple";
System.out,printin(s.indexO('p');
Output:1
lastIndexOf() Returns the position of last occurrence of the specified character in
the string.
Eg String s"apple";
System.out.printin(s.lastlndex Of('p');
Output :2
startswith() Checks whether the string starts with the specificd character or
string or not and returns a boolcan value true or false
Eg String s"apple"
boolean x* s.startswith("A");
System.out.println(x);
Output : false
endswith( ) Checks whether the string cnds with the spccified character or
string or not and returns a boolcan value true or falsc.
Eg String s"appled
boolean x= sstaswith("e");
System.out.println(*);
Output : truc
concat( ) Concats ic combincs two strings
Eg Stringa "Computer", b"Applications"c;
ca.concat(b)
System.out.printin(c);
Output: ComputerApplications
valucOr( ) Converls the string represcntation valueto any primitive data type
Eg String x "12";
int a Integer.valueOf(x);
double Double.valucO(x};
System.oul,printin(n):
System.out.println(b);
Output : 12
12.0
Wrapper class converts the primitive data type into objects
Some of the Wrapper classes are : Byte, Short, Integer, Long, Float ,Double,
Character and Boolean.
Predefined functions of CHARACTER apper class
toUppercase( ) Converts the character which is passed as parameter into
uppercase
char a='x';
a=Character.toUpperCse(a);
System.out.printin(a);
Output : X
toLowerCase( ) Converts the character which is passed as parameter into
lowercase
char a=X';
a=Character.toLowerCse(a);
System.out.printin(a);
Output :X
isUpperCase() Checks whether the varameter passed is an uppercase alphabet
or not and returns value true or false.
char a='x:
boolean b= Character.isUpperCase(a);
System.out.printIn{b):
Output : false;
isLowerCase() Checks whether the parameter passed is a lowercase alphabet
or not and returns value true or false.
char a='x;
boolean b= Character.isLowerCase(a);
System.out.printin(b);
Output: true;
isDigit( ) Checks whether the parameter passed is a digit or not and
returns value true or false.
int a= 20;
boolean b= Character.isDigit(a);
System.out.println(b):;
Output: true;
isWhiteSpace( ) Checks whether the parameter passed is a blank space /white
space and returns value true or false.
char a='e';
boolean b= Character.isWhiteSpace(a);
System.out.printin(b):
Output: false;
isLetterOrDigit( ) Checks whether the parameter passed is an alphabet or digit
and returns value true or false.