SlideShare a Scribd company logo
String Handling
Abhishek S. Rao
• Implementing strings as built-in objects allows Java to provide a full
complement of features that make string handling convenient. For
example, Java has methods to compare two strings, search for a
substring, concatenate two strings, and change the case of letters within
a string.
• Somewhat unexpectedly, when you create a String object, you are
creating a string that cannot be changed. That is, once a String object
has been created, you cannot change the characters that comprise that
string. At first, this may seem to be a serious restriction. However, such
is not the case. You can still perform all types of string operations.
• The difference is that each time you need an altered version of an
existing string, a new String object is created that contains the
modifications. The original string is left unchanged. This approach is
used because fixed, immutable strings can be implemented more
efficiently than changeable ones. For those cases in which a modifiable
string is desired, Java provides two options: StringBuffer and
• The String, StringBuffer, and StringBuilder classes are
defined in java.lang. Thus, they are available to all programs
automatically. All are declared final, which means that none of
these classes may be subclassed. This allows certain
optimizations that increase performance to take place on
common string operations.
The String Constructors
The String class supports several constructors. To create an empty
String, call the default constructor. For example, String s = new
will create an instance of String with no characters in it. Frequently,
you will want to create strings that have initial values. The String class
provides a variety of constructors to handle this. To create a String
initialized by an array of characters, use the constructor shown here:
String(char chars[ ])
Here is an example:
You can specify a subrange of a character array as an initializer
using the following constructor:
String(char chars[ ], int startIndex, int numChars)
Here, startIndex specifies the index at which the subrange begins,
and numChars specifies the number of characters to use. Here is
an example:
String handling
String handling
String Length
The length of a string is the number of characters that it contains.
To obtain this
value, call the length( ) method, shown here:
int length( )
The following fragment prints "3", since there are three
characters in the string s:
Special String Operations
Because strings are a common and important part of programming,
Java has added special support for several string operations within the
syntax of the language. These operations include the automatic creation
of new String instances from string literals, concatenation of multiple
String objects by use of the + operator, and the conversion of other
to a string representation. There are explicit methods available to
perform all of these functions, but Java does them automatically as a
convenience for the programmer and to add clarity.
String Literals
For each string literal in your program, Java automatically constructs a
String object. Thus, you can use a string literal to initialize a String
example, the following code fragment creates two equivalent strings:
String Concatenation
In general, Java does not allow operators to be applied to String
objects. The one exception to this rule is the + operator, which
concatenates two strings, producing a String object as the result.
This allows you to chain together a series of + operations. For
example, the following fragment concatenates three strings:
String handling
String Conversion and toString( )
When Java converts data into its string representation during
concatenation, it does so by calling one of the overloaded versions of the
string conversion method valueOf( ) defined by String. valueOf( ) is
overloaded for all the primitive types and for type Object.
For the primitive types, valueOf( ) returns a string that contains the
human-readable equivalent of the value with which it is called. For
objects, valueOf( ) calls the toString( ) method on the object.
Every class implements toString( ) because it is defined by Object.
However, the default implementation of toString( ) is seldom
sufficient. For most important classes that you create, you will want to
override toString( ) and provide your own string representations.
Fortunately, this is easy to do. The toString( ) method has this general
form:
String toString( )
To implement toString( ), simply return a String object that contains
the humanreadable string that appropriately describes an object of
your class.
By overriding toString( ) for classes that you create, you allow them to
be fully integrated into Java’s programming environment. For example,
they can be used in print( ) and println( ) statements and in
concatenation expressions. The following program demonstrates this
by overriding toString( ) for the Box class:
String handling
Character Extraction
The String class provides a number of ways in which characters can
be extracted from a String object. Several are examined here.
Although the characters that comprise a string within a String object
cannot be indexed as if they were a character array, many of the
String methods employ an index (or offset) into the string for their
operation. Like arrays, the string indexes begin at zero.
String handling
String handling
String handling
String handling
String Comparison
The String class includes a number of methods that compare strings
or substrings within strings.
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
trim( )
The trim( ) method returns a copy of the invoking string from which
which any leading and trailing whitespace has been removed. It has
has this general form:
String trim( )
Here is an example:
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling
String handling

More Related Content

PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PDF
Java String
Java2Blog
 
PPT
Chapter 7 String
OUM SAOKOSAL
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PDF
Arrays string handling java packages
Sardar Alam
 
PPT
String handling session 5
Raja Sekhar
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
String in java
Ideal Eyes Business College
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Java String
Java2Blog
 
Chapter 7 String
OUM SAOKOSAL
 
Java String
SATYAM SHRIVASTAV
 
Arrays string handling java packages
Sardar Alam
 
String handling session 5
Raja Sekhar
 
Strings in Java
Abhilash Nair
 

What's hot (20)

PPTX
L13 string handling(string class)
teach4uin
 
PDF
String handling(string class)
Ravi Kant Sahu
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PPT
String Handling
Bharat17485
 
PPTX
Java Strings
RaBiya Chaudhry
 
PPTX
Java strings
Mohammed Sikander
 
PPT
Strings
naslin prestilda
 
PPTX
L14 string handling(string buffer class)
teach4uin
 
PPT
Wrapper class (130240116056)
Akshay soni
 
PPTX
L9 wrapper classes
teach4uin
 
PPT
STRINGS IN JAVA
LOVELY PROFESSIONAL UNIVERSITY
 
PPTX
What is String in Java?
RAKESH P
 
PPT
Strings v.1.1
BG Java EE Course
 
PPTX
Vectors in Java
Abhilash Nair
 
PPT
wrapper classes
Rajesh Roky
 
PPT
M C6java7
mbruggen
 
PPSX
Strings in Java
Hitesh-Java
 
PPTX
C# Strings
Hock Leng PUAH
 
PDF
Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov
 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
L13 string handling(string class)
teach4uin
 
String handling(string class)
Ravi Kant Sahu
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
String Handling
Bharat17485
 
Java Strings
RaBiya Chaudhry
 
Java strings
Mohammed Sikander
 
L14 string handling(string buffer class)
teach4uin
 
Wrapper class (130240116056)
Akshay soni
 
L9 wrapper classes
teach4uin
 
What is String in Java?
RAKESH P
 
Strings v.1.1
BG Java EE Course
 
Vectors in Java
Abhilash Nair
 
wrapper classes
Rajesh Roky
 
M C6java7
mbruggen
 
Strings in Java
Hitesh-Java
 
C# Strings
Hock Leng PUAH
 
Anton Kasyanov, Introduction to Python, Lecture3
Anton Kasyanov
 
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Ad

Similar to String handling (20)

PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PPTX
String in java, string constructors and operations
manjeshbngowda
 
PPTX
String in JAVA --------------------------
2003sayanch
 
PPTX
Java string handling
Salman Khan
 
PPT
String classes and its methods.20
myrajendra
 
PPTX
Day_5.1.pptx
ishasharma835109
 
PPTX
javastringexample problems using string class
fedcoordinator
 
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
PPSX
String and string manipulation x
Shahjahan Samoon
 
PPTX
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
PPT
String and string manipulation
Shahjahan Samoon
 
PDF
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
DOCX
Java R20 - UNIT-5.docx
Pamarthi Kumar
 
PPT
Text processing
Icancode
 
PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPT
Java căn bản - Chapter9
Vince Vo
 
PPTX
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
PPTX
String.pptx
RanjithKumar742256
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
Java string handling
GaneshKumarKanthiah
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
String in java, string constructors and operations
manjeshbngowda
 
String in JAVA --------------------------
2003sayanch
 
Java string handling
Salman Khan
 
String classes and its methods.20
myrajendra
 
Day_5.1.pptx
ishasharma835109
 
javastringexample problems using string class
fedcoordinator
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
String and string manipulation x
Shahjahan Samoon
 
String Handling, Inheritance, Packages and Interfaces
Prabu U
 
String and string manipulation
Shahjahan Samoon
 
Java R20 - UNIT-5.pdf
Pamarthi Kumar
 
Java R20 - UNIT-5.docx
Pamarthi Kumar
 
Text processing
Icancode
 
Java Strings methods and operations.ppt
JyothiAmpally
 
Java căn bản - Chapter9
Vince Vo
 
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
String.pptx
RanjithKumar742256
 
Ad

Recently uploaded (20)

PDF
Software Development Methodologies in 2025
KodekX
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Software Development Methodologies in 2025
KodekX
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Doc9.....................................
SofiaCollazos
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
This slide provides an overview Technology
mineshkharadi333
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 

String handling

  • 2. • Implementing strings as built-in objects allows Java to provide a full complement of features that make string handling convenient. For example, Java has methods to compare two strings, search for a substring, concatenate two strings, and change the case of letters within a string. • Somewhat unexpectedly, when you create a String object, you are creating a string that cannot be changed. That is, once a String object has been created, you cannot change the characters that comprise that string. At first, this may seem to be a serious restriction. However, such is not the case. You can still perform all types of string operations. • The difference is that each time you need an altered version of an existing string, a new String object is created that contains the modifications. The original string is left unchanged. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, Java provides two options: StringBuffer and
  • 3. • The String, StringBuffer, and StringBuilder classes are defined in java.lang. Thus, they are available to all programs automatically. All are declared final, which means that none of these classes may be subclassed. This allows certain optimizations that increase performance to take place on common string operations.
  • 4. The String Constructors The String class supports several constructors. To create an empty String, call the default constructor. For example, String s = new will create an instance of String with no characters in it. Frequently, you will want to create strings that have initial values. The String class provides a variety of constructors to handle this. To create a String initialized by an array of characters, use the constructor shown here: String(char chars[ ]) Here is an example:
  • 5. You can specify a subrange of a character array as an initializer using the following constructor: String(char chars[ ], int startIndex, int numChars) Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example:
  • 8. String Length The length of a string is the number of characters that it contains. To obtain this value, call the length( ) method, shown here: int length( ) The following fragment prints "3", since there are three characters in the string s:
  • 9. Special String Operations Because strings are a common and important part of programming, Java has added special support for several string operations within the syntax of the language. These operations include the automatic creation of new String instances from string literals, concatenation of multiple String objects by use of the + operator, and the conversion of other to a string representation. There are explicit methods available to perform all of these functions, but Java does them automatically as a convenience for the programmer and to add clarity.
  • 10. String Literals For each string literal in your program, Java automatically constructs a String object. Thus, you can use a string literal to initialize a String example, the following code fragment creates two equivalent strings:
  • 11. String Concatenation In general, Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. This allows you to chain together a series of + operations. For example, the following fragment concatenates three strings:
  • 13. String Conversion and toString( ) When Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf( ) defined by String. valueOf( ) is overloaded for all the primitive types and for type Object. For the primitive types, valueOf( ) returns a string that contains the human-readable equivalent of the value with which it is called. For objects, valueOf( ) calls the toString( ) method on the object.
  • 14. Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient. For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this general form: String toString( ) To implement toString( ), simply return a String object that contains the humanreadable string that appropriately describes an object of your class. By overriding toString( ) for classes that you create, you allow them to be fully integrated into Java’s programming environment. For example, they can be used in print( ) and println( ) statements and in concatenation expressions. The following program demonstrates this by overriding toString( ) for the Box class:
  • 16. Character Extraction The String class provides a number of ways in which characters can be extracted from a String object. Several are examined here. Although the characters that comprise a string within a String object cannot be indexed as if they were a character array, many of the String methods employ an index (or offset) into the string for their operation. Like arrays, the string indexes begin at zero.
  • 21. String Comparison The String class includes a number of methods that compare strings or substrings within strings.
  • 39. trim( ) The trim( ) method returns a copy of the invoking string from which which any leading and trailing whitespace has been removed. It has has this general form: String trim( ) Here is an example: