SlideShare una empresa de Scribd logo
1 de 21
Descargar para leer sin conexión
Programming in Java
Lecture 13: StringBuffer
By
Ravi Kant Sahu
Asst. Professor
Lovely Professional University, PunjabLovely Professional University, Punjab
Introduction
 A string buffer implements a mutable sequence of characters.
 A string buffer is like a String, but can be modified.
 At any point in time it contains some particular sequence of
characters, but the length and content of the sequence can be
changed through certain method calls.
 Every string buffer has a capacity.
 When the length of the character sequence contained in the
string buffer exceed the capacity, it is automatically made
larger.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Introduction
 String buffers are used by the compiler to implement the
binary string concatenation operator +.
 For example:
x = "a" + 4 + "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).
append("c") .toString()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Why StringBuffer?
 StringBuffer is a peer class of String that provides much of the
functionality of strings.
 String represents fixed-length, immutable character sequences. In
contrast, StringBuffer represents growable and writeable character
sequences.
 StringBuffer may have characters and substrings inserted in the
middle or appended to the end.
 StringBuffer will automatically grow to make room for such
additions.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
StringBuffer Constructors
 StringBuffer defines these four constructors:
◦ StringBuffer( )
◦ StringBuffer(int size)
◦ StringBuffer(String str)
◦ StringBuffer(CharSequence chars)
 The default constructor reserves room for 16 characters without
reallocation.
 By allocating room for a few extra characters(size +16),
StringBuffer reduces the number of reallocations that take place.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
StringBuffer Methods
length( ) and capacity( )
The current length of a StringBuffer can be found via the length( ) method,
while the total allocated capacity can be found through the capacity( )
method.
int length( )
int capacity( )
Example:
class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer(“New Zealand");
System.out.println("length = " + sb.length());
System.out.println("capacity = " + sb.capacity());
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
ensureCapacity( )
 If we want to preallocate room for a certain number of
characters after a StringBuffer has been constructed, we can
use ensureCapacity( ) to set the size of the buffer.
 This is useful if we know in advance that we will be
appending a large number of small strings to a StringBuffer.
void ensureCapacity(int capacity)
 Here, capacity specifies the size of the buffer.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
setLength( )
 used to set the length of the buffer within a StringBuffer object.
void setLength(int length)
 Here, length specifies the length of the buffer.
 When we increase the size of the buffer, null characters are added to
the end of the existing buffer.
 If we call setLength( ) with a value less than the current value
returned by length( ), then the characters stored beyond the new
length will be lost.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
charAt( ) and setCharAt( )
 The value of a single character can be obtained from a
StringBuffer via the charAt( ) method.
 We can set the value of a character within a StringBuffer using
setCharAt( ).
char charAt(int index)
void setCharAt(int index, char ch)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
getChars( )
 getChars( ) method is used to copy a substring of a
StringBuffer into an array.
void getChars(int sourceStart, int sourceEnd, char target[ ],
int targetStart)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
append( )
 The append( ) method concatenates the string representation
of any other type of data to the end of the invoking
StringBuffer object.
 It has several overloaded versions.
◦ StringBuffer append(String str)
◦ StringBuffer append(int num)
◦ StringBuffer append(Object obj)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append("a = ").append(a).append("!")
.toString();
System.out.println(s);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
insert( )
 The insert( ) method inserts one string into another.
 It is overloaded to accept values of all the simple types, plus
Strings, Objects, and CharSequences.
 This string is then inserted into the invoking StringBuffer
object.
◦ StringBuffer insert(int index, String str)
◦ StringBuffer insert(int index, char ch)
◦ StringBuffer insert(int index, Object obj)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
reverse( )
 Used to reverse the characters within a StringBuffer object.
 This method returns the reversed object on which it was
called.
Example:
class ReverseDemo {
public static void main(String args[]) {
StringBuffer s = new StringBuffer(“Banana");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
delete( ) and deleteCharAt( )
 Used to delete characters within a StringBuffer.
StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int index)
 The delete( ) method deletes a sequence of characters from the
invoking object.
 The deleteCharAt( ) method deletes the character at the
specified index.
 It returns the resulting StringBuffer object.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class deleteDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer(“She is not a good girl.”);
sb.delete(7, 11);
System.out.println("After delete: " + sb);
sb.deleteCharAt(7);
System.out.println("After deleteCharAt: " + sb);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
replace( )
 Used to replace one set of characters with another set inside a
StringBuffer object.
StringBuffer replace(int startIndex, int endIndex, String str)
 The substring being replaced is specified by the indexes startIndex
and endIndex.
 Thus, the substring at startIndex through endIndex–1 is replaced.
 The replacement string is passed in str.
 The resulting StringBuffer object is returned.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class replaceDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
substring( )
 Used to obtain a portion of a StringBuffer by calling substring( ).
String substring(int startIndex)
String substring(int startIndex, int endIndex)
 The first form returns the substring that starts at startIndex and
runs to the end of the invoking StringBuffer object.
 The second form returns the substring that starts at startIndex
and runs through endIndex–1.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Array
ArrayArray
Array
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Generics
GenericsGenerics
Generics
 
L9 wrapper classes
L9 wrapper classesL9 wrapper classes
L9 wrapper classes
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java - Strings Concepts
Java - Strings ConceptsJava - Strings Concepts
Java - Strings Concepts
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
wrapper classes
wrapper classeswrapper classes
wrapper classes
 
Java Strings
Java StringsJava Strings
Java Strings
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
String in java
String in javaString in java
String in java
 
Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 

Destacado

Destacado (20)

String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
StringTokenizer in java
StringTokenizer in javaStringTokenizer in java
StringTokenizer in java
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Basic IO
Basic IOBasic IO
Basic IO
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Exception handling
Exception handlingException handling
Exception handling
 
String operation
String operationString operation
String operation
 
Event handling
Event handlingEvent handling
Event handling
 
String Handling
String HandlingString Handling
String Handling
 
Packages
PackagesPackages
Packages
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
C programming & data structure [character strings & string functions]
C programming & data structure   [character strings & string functions]C programming & data structure   [character strings & string functions]
C programming & data structure [character strings & string functions]
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Applets
AppletsApplets
Applets
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 

Similar a String handling(string buffer class)

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)teach4uin
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesPrabu U
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptxVeenaNaik23
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper classSimoniShah6
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...Indu32
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string classfedcoordinator
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdfAnanthi68
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Danial Virk
 
String Operations.pptx
String Operations.pptxString Operations.pptx
String Operations.pptxpateljay401233
 

Similar a String handling(string buffer class) (20)

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
StringBuffer.pptx
StringBuffer.pptxStringBuffer.pptx
StringBuffer.pptx
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 
package
packagepackage
package
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
 
07slide
07slide07slide
07slide
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
Java
JavaJava
Java
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
 
8. String
8. String8. String
8. String
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
 
String handling
String handlingString handling
String handling
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
String Operations.pptx
String Operations.pptxString Operations.pptx
String Operations.pptx
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Md08 collection api
Md08 collection apiMd08 collection api
Md08 collection api
 

Más de Ravi_Kant_Sahu

Más de Ravi_Kant_Sahu (8)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Event handling
Event handlingEvent handling
Event handling
 
List classes
List classesList classes
List classes
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Jdbc
JdbcJdbc
Jdbc
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
Swing api
Swing apiSwing api
Swing api
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 

Último

5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...Apsara Of India
 
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24... Shivani Pandey
 
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser... Shivani Pandey
 
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Riya Pathan
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...noor ahmed
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Call Girls in Nagpur High Profile
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Riya Pathan
 
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...aamir
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...russian goa call girl and escorts service
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...rahim quresi
 
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...noor ahmed
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceNitya salvi
 
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...aamir
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.Nitya salvi
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 

Último (20)

5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
 
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
Model Call Girls In Ariyalur WhatsApp Booking 7427069034 call girl service 24...
 
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Velappanchavadi WhatsApp Booking 7427069034 call girl ser...
 
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Garulia Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
 
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Manjri Call Me 7737669865 Budget Friendly No Advance Booking
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
Independent Hatiara Escorts ✔ 9332606886✔ Full Night With Room Online Booking...
 
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
 
Goa Call "Girls Service 9316020077 Call "Girls in Goa
Goa Call "Girls  Service   9316020077 Call "Girls in GoaGoa Call "Girls  Service   9316020077 Call "Girls in Goa
Goa Call "Girls Service 9316020077 Call "Girls in Goa
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
 
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Howrah ⟟ 8250192130 ⟟ High Class Call Girl In...
 
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls ServiceCollege Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
College Call Girls Pune 8617697112 Short 1500 Night 6000 Best call girls Service
 
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
Nayabad Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Sex At ...
 
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort GoaDesi Bhabhi Call Girls  In Goa  💃 730 02 72 001💃desi Bhabhi Escort Goa
Desi Bhabhi Call Girls In Goa 💃 730 02 72 001💃desi Bhabhi Escort Goa
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
 

String handling(string buffer class)

  • 1. Programming in Java Lecture 13: StringBuffer By Ravi Kant Sahu Asst. Professor Lovely Professional University, PunjabLovely Professional University, Punjab
  • 2. Introduction  A string buffer implements a mutable sequence of characters.  A string buffer is like a String, but can be modified.  At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.  Every string buffer has a capacity.  When the length of the character sequence contained in the string buffer exceed the capacity, it is automatically made larger. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Introduction  String buffers are used by the compiler to implement the binary string concatenation operator +.  For example: x = "a" + 4 + "c" is compiled to the equivalent of: x = new StringBuffer().append("a").append(4). append("c") .toString() Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. Why StringBuffer?  StringBuffer is a peer class of String that provides much of the functionality of strings.  String represents fixed-length, immutable character sequences. In contrast, StringBuffer represents growable and writeable character sequences.  StringBuffer may have characters and substrings inserted in the middle or appended to the end.  StringBuffer will automatically grow to make room for such additions. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. StringBuffer Constructors  StringBuffer defines these four constructors: ◦ StringBuffer( ) ◦ StringBuffer(int size) ◦ StringBuffer(String str) ◦ StringBuffer(CharSequence chars)  The default constructor reserves room for 16 characters without reallocation.  By allocating room for a few extra characters(size +16), StringBuffer reduces the number of reallocations that take place. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. StringBuffer Methods length( ) and capacity( ) The current length of a StringBuffer can be found via the length( ) method, while the total allocated capacity can be found through the capacity( ) method. int length( ) int capacity( ) Example: class StringBufferDemo { public static void main(String args[]) { StringBuffer sb = new StringBuffer(“New Zealand"); System.out.println("length = " + sb.length()); System.out.println("capacity = " + sb.capacity()); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. ensureCapacity( )  If we want to preallocate room for a certain number of characters after a StringBuffer has been constructed, we can use ensureCapacity( ) to set the size of the buffer.  This is useful if we know in advance that we will be appending a large number of small strings to a StringBuffer. void ensureCapacity(int capacity)  Here, capacity specifies the size of the buffer. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. setLength( )  used to set the length of the buffer within a StringBuffer object. void setLength(int length)  Here, length specifies the length of the buffer.  When we increase the size of the buffer, null characters are added to the end of the existing buffer.  If we call setLength( ) with a value less than the current value returned by length( ), then the characters stored beyond the new length will be lost. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. charAt( ) and setCharAt( )  The value of a single character can be obtained from a StringBuffer via the charAt( ) method.  We can set the value of a character within a StringBuffer using setCharAt( ). char charAt(int index) void setCharAt(int index, char ch) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. getChars( )  getChars( ) method is used to copy a substring of a StringBuffer into an array. void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. append( )  The append( ) method concatenates the string representation of any other type of data to the end of the invoking StringBuffer object.  It has several overloaded versions. ◦ StringBuffer append(String str) ◦ StringBuffer append(int num) ◦ StringBuffer append(Object obj) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Example class appendDemo { public static void main(String args[]) { String s; int a = 42; StringBuffer sb = new StringBuffer(40); s = sb.append("a = ").append(a).append("!") .toString(); System.out.println(s); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. insert( )  The insert( ) method inserts one string into another.  It is overloaded to accept values of all the simple types, plus Strings, Objects, and CharSequences.  This string is then inserted into the invoking StringBuffer object. ◦ StringBuffer insert(int index, String str) ◦ StringBuffer insert(int index, char ch) ◦ StringBuffer insert(int index, Object obj) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 14. Example class insertDemo { public static void main(String args[]) { StringBuffer sb = new StringBuffer("I Java!"); sb.insert(2, "like "); System.out.println(sb); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 15. reverse( )  Used to reverse the characters within a StringBuffer object.  This method returns the reversed object on which it was called. Example: class ReverseDemo { public static void main(String args[]) { StringBuffer s = new StringBuffer(“Banana"); System.out.println(s); s.reverse(); System.out.println(s); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 16. delete( ) and deleteCharAt( )  Used to delete characters within a StringBuffer. StringBuffer delete(int startIndex, int endIndex) StringBuffer deleteCharAt(int index)  The delete( ) method deletes a sequence of characters from the invoking object.  The deleteCharAt( ) method deletes the character at the specified index.  It returns the resulting StringBuffer object. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 17. Example class deleteDemo { public static void main(String args[]) { StringBuffer sb = new StringBuffer(“She is not a good girl.”); sb.delete(7, 11); System.out.println("After delete: " + sb); sb.deleteCharAt(7); System.out.println("After deleteCharAt: " + sb); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 18. replace( )  Used to replace one set of characters with another set inside a StringBuffer object. StringBuffer replace(int startIndex, int endIndex, String str)  The substring being replaced is specified by the indexes startIndex and endIndex.  Thus, the substring at startIndex through endIndex–1 is replaced.  The replacement string is passed in str.  The resulting StringBuffer object is returned. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 19. Example class replaceDemo { public static void main(String args[]) { StringBuffer sb = new StringBuffer("This is a test."); sb.replace(5, 7, "was"); System.out.println("After replace: " + sb); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 20. substring( )  Used to obtain a portion of a StringBuffer by calling substring( ). String substring(int startIndex) String substring(int startIndex, int endIndex)  The first form returns the substring that starts at startIndex and runs to the end of the invoking StringBuffer object.  The second form returns the substring that starts at startIndex and runs through endIndex–1. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)