String Handling
String Handling
• Introduction
• Immutable class in Java
• String Class in Java
• Methods of String Class
• String Compare in Java
• String Concatenation in Java
• StringBuilder
2
Introduction
The basic aim of String Handling concept is storing the string data in
the main memory (RAM), manipulating the data of the String,
retrieving the part of the String etc. String Handling provides a lot of
concepts that can be performed on a string such as concatenation of
string, comparison of string, find sub string etc.
Java String contains an immutable sequence of Unicode characters.
Java String is differ from string in C or C++, where (in C or C++)
string is simply an array of char. String class is encapsulated
under java.lang package.
3
Immutable class in Java
String:
String is a sequence of characters enclosed within double quotes (" ") is known as String.
Example: "Java Programming".
In java programming to store the character data we have a fundamental datatype called char.
Similarly to store the string data and to perform various operation on String data, we have three
predefined classes they are:
String
StringBuffer
StringBuilder
5
String Class in Java
It is a predefined class in java.lang package can be used to handle the String. String class is immutable that means whose
content can not be changed at the time of execution of program.
String class object is immutable that means when we create an object of String class it never changes in the existing object.
6
Methods of String class
7
toLowerCase()
toLowerCase(): This method is used to
convert lower case string into upper case
Example:-
endsWith() subString() indexOf()
endsWith(): This method return true if string subString(): This method is used to get the indexOf(): This method is used find the index
is end with given another string, otherwise it part of given string. value of given string. It always gives starting index
returns false. value of first occurrence of string.
Example:-
Example:- Example:-
lastIndexOf() trim()
lastIndexOf(): This method used to return the starting index value trim(): This method remove space which are available
of last occurrence of the given string.
Example:-
before starting of string and after ending of string.
Example:-
9
split() replace()
split(): This method is used to divide the given string into replace(): This method is used to return a duplicate string
number of parts based on delimiter (special symbols like by replacing old character with new character.
@ space , ).
Example:-
Example:-
10
String Compare in Java
== or Double Equals
12
• compareTo() Method in Java
13
String Concatenation
There are two way to concat string object in java:
• By + (string concatenation) operator
• By concat() method
• By concat() method
14
StringBuilder
It is a predefined class in java.lang package can be used to handle the String. StringBuilder class is almost similar to to StringBuffer
class. It is also a mutable object.
The main difference StringBuffer and StringBuilder class is StringBuffer is thread safe that means only one threads allowed at a time to
work on the String where as StringBuilder is not thread safe that means multiple threads can work on same String value.
All the things between StringBuffer and StringBuilder are same only difference is StringBuffer is synchronized and StringBuilder is not synchronized.
synchronized means one thread is allow at a time so it thread safe. Not synchronized means multiple threads are allow at a time so it not thread safe.
15
When we use String, StringBuffer and StringBuilder
1. If the content is fixed and would not change frequently then we use String.
2. If content is not fixed and keep on changing but thread safety is required then we use StringBuffer
3. If content is not fixed and keep on changing and thread safety is not required then we use StringBuilder
16
THANK YOU