String
Prof. Debashis Hati
Email :dhatifcs@[Link]
Cell No: 9437028209/ 6370961683
07-JAN-2022 Prof. Debashis Hati
String
It is a predefined class and is available in [Link].
final class
The content of a string can’t be changed.
Another class is available , StringBuffer whose content can be changed.
Constructors
1. String s1= new String();
Empty string
[Link](s1);
2. char x[ ]= {‘a’,’b’};
String s2= new String(x);
[Link](s2);
3. String s3= new String(x);
[Link](s3);
4. Byte a[ ]= {65,66};
String s4= new String(a);
[Link](s4);
String
length() method
It is used to return the no. of characters of the string.
String concatenation
Java does not allow operators with string objects , only exception is +
which concatenates two strings.
int x=10;
[Link](x); // 10
String age=“20”;
String y= “ He is “+ age+”years old”;
[Link](y);
String z=“ He is “+ x+”years old”
[Link](z);
Data conversion with valueOf()
static String valueOf(byte/short/int/long)
static String valueOf(float/double)
static String valueOf(char)
static String valueOf(boolean)
static String valueOf(Object) valueOf() calls toString( ) methods which
returns the description of the object.
The class Object is the super/base of all predefined and user defined
classes. The Object class has a String toString() method and that
method can be overridden by user defined class.
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
[Link]([Link]);
[Link]([Link]);
[Link](m);
}}
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
[Link]([Link]);
[Link]([Link]);
[Link]( “Rs”+ [Link]+”.”+[Link]+”paisa”);
}} Rs 10.50 paisa
Use of toString() method
class Money {
int rupee;
int paisa;
Money(){
rupee=0;
paisa=0;
}
Money(int x,int y){
rupee=x;
paisa=y;
}}
String toString(){
String z= “Rs”+rupee+”.”+paisa+”paisa”;
retrun z;} }
class Demo{
public static void main(String t[]){
Money m=new Money(10,50);
[Link](m);}} Rs 10.50 paisa
Methods of String class
1. Character extraction
int charAt(int index);
char x=“abc”.charAt(1);
[Link](x);
char [ ] toCharArray();
2. String comparison
boolean equals(Object );
boolean equalsIgnoreCase(Object );
String s1=“Hello”;
String s2=new String(“Hello”);
String s3=“HELLO”;
[Link]([Link](s2));
[Link]([Link](s3));
[Link]([Link](s3));
String s4=s1;
Methods of String class
equals() versus ==
[Link](s1==s2);
[Link](s1==s4);
== OPERATOR
String s1=“hello”;
String s2=“hello”;
[Link](s1==s2);
String s1=“hello”;
String s2= new String(“hello”);
[Link](s1==s2);
String s1= new String(“hello”);
String s2= new String(“hello”);
[Link](s1==s2);
Methods of String class
int compareTo(String str);
Less than zero : invoking string is less than str.
Greater than zero : invoking string is greater than str.
Zero : Two strings are equal.
String str[ ]= { , , ,};
for (i=0; i < [Link] ; i++)
for (j=i+1; ij< [Link] ; j++)
if (str[i].compareTo(str[j]) < 0
String temp= str[i];
str[i]=str[j];
str[j]= temp;
Methods of String class
3. Searching string
int indexOf(char) searching the 1st occurrence of a character
int lastIndexOf(char)searching the last occurrence of a
character
String s=“ KIIT”;
[Link]( [Link](‘I’)); // 1
[Link]( [Link](‘I’)); //2
4. substring( int startindex); “KIIT”.subString(1);
substring( int startindex,int lastindex); “KIIT”.subString(1,2);
5. concat(String str);
String s1=“one”;
String s2= [Link](“two”);
6. replace( char x,char y);
“Hello”.replace(‘l’, ‘w’);
7. trim() removing leading and trailing white spaces
StringBuffer class
Supports a growable / modifiable string
Constructors
1. StringBuffer() reserves for 16 characters
2. StringBuffer(int size) explictly specifying the size
3. StringBuffer(String s) from a string.
length() and capacity() methods
StringBuffer s1= new StringBuffer();
[Link]([Link]()); // 0
[Link]([Link]()); // 16
StringBuffer s2= new StringBuffer(10);
[Link]([Link]()); // 4
[Link]([Link]()); // 10
StringBuffer s3= new StringBuffer(“kiit);
[Link]([Link]()); // 4
[Link]([Link]() );// 20
StringBuffer class
char charAt(int index);
void setCharAt(int index)
StringBuffer insert(int startindex, String str);
StringBuffer insert(int startindex, char c);
StringBuffer insert(int startindex, Object obj);
StringBuffer reverse()
StringBuffer delete(int start, int end);