String Handling
String Handling
String class:- String is a composite datatype which is used to store one or more than
one character enclosed in double quotes(“ “).
String is a pre-defined class present in java.lang package.
Declaration of String:-
1. String a= new String();
2. String b;
Initialization of String :-
1. String a= new String(“Computer Application”);
2. String b=”Computer Application”;
Functions of Scanner class used for taking String as Input:-
1. next()- to input single word(without space);
2. nextLine()- to input one or more than one word.
String
This function is used to join one String to s1=”Computer”,s2=”Application”;
String concat(String str)
other. String r=s1.concat(s2);
r=”ComputerApplication”;
This function is used to check whether two
String
Strings are equal (same) or not. This
s1=”Computer”,s2=”computer”;
boolean equals(String str) function is case sensitive i.e if alphabets
boolean r=s1.equals(s2);
are same in both String but their cases
r=false;
differ then this function will return false.
This function is used to check whether two
Strings are equal(same) or not. This
String
function is not case sensitive i.e if
boolean s1=”Computer”,s2=”computer”;
alphabets are same in both String but
equalsIgnoreCase(String str) boolean r=s1.equalsIgnoreCase(s2);
their cases differ then this function will
r=true;
return true.
Note-This function is not case sensitive.
1. String
s1=”Computer”,s2=”comput
er”;
This function compares two String
int r= s1.compareTo(s2);
lexicographically.
r= -32;
Both String same gives 0
2. String
First String if gives
s1=”Compute”,s2=”Comput
greater(Alphabetical positive
er”;
order or value
int r= s1.compareTo(s2);
int compareTo(String str) lexicographically)than
r= -1 ;
second
3. String
First String if gives
s1=”Computer”,s2=”Compu
smaller(Alphabetical negative
order or value te”;
lexicographically)than
int r= s1.compareTo(s2);
second
r= 1 ;
int a=7832;
this function is used to convert any value
String valueOf(AnyDatatype) String s=String.valueOf(a);
of any datatype into String
now s=”7832”;