Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
179 views

String Handling

This document discusses string handling in Java. It covers immutable classes like String, the String class and its methods like length(), charAt(), compareTo(), and concat(). It also discusses comparing strings using equals(), ==, and compareTo() and concatenating strings using + and concat(). The document explains StringBuilder and when to use String, StringBuffer, and StringBuilder.

Uploaded by

Anirban Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

String Handling

This document discusses string handling in Java. It covers immutable classes like String, the String class and its methods like length(), charAt(), compareTo(), and concat(). It also discusses comparing strings using equals(), ==, and compareTo() and concatenating strings using + and concat(). The document explains StringBuilder and when to use String, StringBuffer, and StringBuilder.

Uploaded by

Anirban Guha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

String Handling

Pailan College Of Management and Technology


Name- Anirban Guha
Roll- 29701219011
Sem- 4th
Sub- Programming in Java
Contents

• 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

Immutable class means that once an object is created,


we cannot change its content. In Java, . In Java String,
Integer, Byte, Short, Float, Double and all other
wrapper classes are immutable.
Character:
It is an identifier enclosed within single quotes (' ').
Example: 'A', '$', 'p'

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

length() charAt(index) toUpperCase()


length(): This method is used to get charAt(): This method is used to get the toUpperCase(): This method is use to
the number of character of any string. character at a given index value. convert lower case string into upper case.

Example:- Example:- Example:-

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

There are three way to compare string object in java:


•By equals() method
•By == operator
•By compareTo() method

• equals() Method in Java

equals() method always used to


comparing contents of both source
and destination String. It return
true if both string are same in
meaning and case otherwise it
11
returns false. It is case sensitive
• == or Double Equals to Operator
in Java

== Operator is always used for comparing


references of both source and destination
objects but not their contents.

== or Double Equals

12
• compareTo() Method in Java

comapreTo() method can be used to compare two string by


taking Unicode values. It returns 0 if the string are same
otherwise returns either +ve or -ve integer.

13
String Concatenation
There are two way to concat string object in java:
• By + (string concatenation) operator
• By concat() method

• By concat() method

concat() method is used to combined two strings.

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.

Difference between StringBuffer and StringBuilder

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

You might also like