17-10-25:
String-StringBuffer- StringBuilder:
In Java, we can handle the Strings in 3 different ways:
1. String:
String class is immutable (can’t be changed).
Methods:
i. length(): It returns the length of the String.
ii. toLowerCase(): To convert the string into the
lowercase.
iii. toUpperCase(): to convert the string into
uppercase.
iv. replace(char old, char new): It can replace the old
character with new character.
v. equals(): It will compare the content of the strings
with case sensitive.
vi. equalsIgnoreCase(): It will compare the content of
the strings without case sensitive.
vii. trim(): To remove the space before and after of the
string.
viii. charAt(int index) : It returns the character at the
specified index.
ix. indexOf(char c1):It returns the 1st occurrence of
the specified character in the given string.
x. lastIndexOf(char c1): It returns the 1st occurrence
of the specified character in the given string from
backside.
xi. split(delimiter): It can spilt the string by using the
delimiter.
Ex:
import [Link].*;
class StringEx
{
public static void main(String s[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter the str1 = ");
String str1=[Link]();
[Link]("The original String = "+str1);
[Link]("str1 length = "+[Link]());
//String str1u = [Link]();
[Link]("[Link]() =
"+[Link]());
[Link]("[Link]('u','U') =
"+[Link]('u','U'));
[Link]("Enter the str2 = ");
String str2=[Link]();
[Link]("[Link](str2) =
"+[Link](str2));
[Link]("[Link](str2) =
"+[Link](str2));
String s3=" gietu ";
[Link]("s3 ="+s3+" [Link]()=
"+[Link]());
[Link]("After [Link]()=
"+[Link]().length());
[Link]("[Link](2)= "+[Link](5));
[Link]("[Link]('a')=
"+[Link]('a'));
[Link]("[Link]('a')=
"+[Link]('a'));
String s4="GIETU is, the best, univeristy";
[Link]("s4= "+s4);
String[] words= [Link](",");
for(String w : words)
[Link](w);
}
}
Output:
D:\GVSN\CSE F\Inneer classes>javac [Link]
D:\GVSN\CSE F\Inneer classes>java StringEx
Enter the str1 =
gvsn
The original String = gvsn
str1 length = 4
[Link]() = GVSN
[Link]('u','U') = gvsn
Enter the str2 =
gvsn
[Link](str2) = true
[Link](str2) = true
s3 = gietu [Link]()= 11
After [Link]()= 5
[Link](2)= e
[Link]('a')= -1
[Link]('a')= -1
s4= GIETU is, the best, univeristy
GIETU is
the best
univeristy
2. StringBuffer:
i. StringBuffer class is the mutable (We can
change).
ii. StringBuffer class’s methods are synchronized
means only one thread can access the method at a
time.
iii. It is called thread safe.
Methods:
i. append(): To add a string at the end of the another
string.
ii. insert(int index, String str): To insert the specified
string to another string at the specified index
position.
iii. delete(int start, int end): To delete the specified
string from start to end.
iv. substring(int start, int end):
v. length():
vi. replace(int start, int end, String str): To replace the
specified String from the starting index to ending
index.
vii. reverse():To reverse the given string.
Ex:
class StringBufferEx
{
public static void main(String s[])
{
StringBuffer s1=new StringBuffer("GIET");
[Link]("s1= "+s1);
[Link](" University");
[Link]("after append,s1= "+s1);
[Link](4, " GUNUPUR");
[Link]("after insert,s1= "+s1);
[Link](5,12,"Rayagada");
[Link]("after replace,s1= "+s1);
[Link]("reverse of s1= "+[Link]());
[Link]("length of s1= "+[Link]());
}
}
Output:
D:\GVSN\CSE F\Inneer classes>javac
[Link]
D:\GVSN\CSE F\Inneer classes>java StringBufferEx
s1= GIET
after append,s1= GIET University
after insert,s1= GIET GUNUPUR University
after replace,s1= GIET Rayagada University
reverse of s1= ytisrevinU adagayaR TEIG
length of s1= 24
3. StringBuilder:
i. StringBuilder class is the mutable (We can
change).
ii. StringBuilder class’s methods are not
synchronized means many threads can access the
method at a time. It is not thread safe.
Methods:
i. append(): To add a string at the end of the another
string.
ii. insert(int index, String str): To insert the specified
string to another string at the specified index
position.
viii. delete(int start, int end): To delete the specified
string from start to end.
ix. substring(int start, int end):
x. length():
xi. replace(int start, int end, String str): To replace the
specified String from the starting index to ending
index.
xii. reverse():To reverse the given string.
Ex:
class StringBuilderEx
{
public static void main(String s[])
{
StringBuilder s1=new StringBuilder("GIET");
[Link]("s1= "+s1);
[Link](" University");
[Link]("after append,s1= "+s1);
[Link](4, " GUNUPUR");
[Link]("after insert,s1= "+s1);
[Link](5,12,"Rayagada");
[Link]("after replace,s1= "+s1);
[Link]("reverse of s1= "+[Link]());
[Link]("length of s1= "+[Link]());
}
}
Output:
D:\GVSN\CSE F\Inneer classes>javac
[Link]
D:\GVSN\CSE F\Inneer classes>java StringBuilderEx
s1= GIET
after append,s1= GIET University
after insert,s1= GIET GUNUPUR University
after replace,s1= GIET Rayagada University
reverse of s1= ytisrevinU adagayaR TEIG
length of s1= 24