STRINGS computer notes
STRINGS computer notes
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
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
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")
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