Skip to content
codingtube

codingtube

Coding and Programming tutorials

  • javascript
  • React
  • ES6
  • React js
  • coding
  • ffmpeg
  • java
  • programming
  • information
  • coding
  • Privacy Policy
  • Twitter trends
  • Age Calculatore
  • Codingtube Community
  • YouTube Tags Generator
  • About
  • Toggle search form

StringBuffer in java

Posted on October 11, 2021October 11, 2021 By christo No Comments on StringBuffer in java

StringBuffer is a peer class of String that provides much of the functionality of strings. As you know, 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 addition

Table of Contents

  • StringBuffer Constructors
  • length( ) and capacity( )
  • StringBuffer length vs. capacity
  • append( )
  • insert( )
  • Demonstrate insert()
  • reverse( )
  • delete( ) and deleteCharAt( )
  • substring( )
  • Additional StringBuffer Methods

StringBuffer Constructors

StringBuffer defines these four constructors:

StringBuffer( )
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)

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. They have the following general forms:

int length( )
int capacity( )

StringBuffer length vs. capacity

class StringBufferDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
System.out.println("buffer = " + sb);
System.out.println("length = " + sb.length());
System.out.println("capacity = " + sb.capacity());
}
}

buffer = Hello
length = 5
capacity = 21

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

Demonstrate append()

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);
}
}

Output

a = 42!

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. Like append( ), it calls String.valueOf( ) to obtain the string representation of the value it is called with. This string is then inserted into the invoking StringBuffer object. These are a few of its forms

StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)

Demonstrate insert()

class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}

Output

I like Java!

reverse( )

Using reverse() to reverse a StringBuffer

class ReverseDemo {
public static void main(String args[]) {
StringBuffer s = new StringBuffer("abcdef");
System.out.println(s);
s.reverse();
System.out.println(s);
}
}

output produced by the program

abcdef
fedcba

delete( ) and deleteCharAt( )

Demonstrate delete() and deleteCharAt()

class deleteDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.delete(4, 7);
System.out.println("After delete: " + sb);
sb.deleteCharAt(0);
System.out.println("After deleteCharAt: " + sb);
}
}

After delete: This a test.
After deleteCharAt: his a test.

replace( )

Demonstrate replace()

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);
}
}

Output

After replace: This was a test

substring( )

String substring(int startIndex)
String substring(int startIndex, int endIndex)

Additional StringBuffer Methods

MethodDescription
StringBuffer appendCodePoint(int ch)Appends a Unicode code point to the end of the invoking object.
A reference to the object is returned. Added by J2SE 5.
int codePointAt(int i)Returns the Unicode code point at the location specified by i.
Added by J2SE 5.
int codePointBefore(int i)Returns the Unicode code point at the location that precedes
that specified by i. Added by J2SE 5.
int codePointCount(int start, int end)Returns the number of code points in the portion of the invoking
String that are between start and end–1. Added by J2SE 5
int indexOf(String str)Searches the invoking StringBuffer for the first occurrence of str.
Returns the index of the match, or –1 if no match is found.
int indexOf(String str, int startIndex)Searches the invoking StringBuffer for the first occurrence of str,
beginning at startIndex. Returns the index of the match, or –1
if no match is found.
int lastIndexOf(String str)Searches the invoking StringBuffer for the last occurrence of str.
Returns the index of the match, or –1 if no match is found.
int lastIndexOf(String str, int startIndexSearches the invoking StringBuffer for the last occurrence of str,
beginning at startIndex. Returns the index of the match, or –1
if no match is found
int offsetByCodePoints(int start, int num)Returns the index with the invoking string that is num code
points beyond the starting index specified by start. Added by
J2SE 5.
CharSequence
subSequence(int startIndex,
int stopIndex)
Returns a substring of the invoking string, beginning at
startIndex and stopping at stopIndex. This method is required
by the CharSequence interface, which is now implemented by
StringBuffer
void trimToSize( )Reduces the size of the character buffer for the invoking object
to exactly fit the current contents. Added by J2SE 5.

java Tags:java, StringBuffer in java

Post navigation

Previous Post: getBytes( ) in java
Next Post: The Collection Classes in java

Related Posts

initialization of Array in java java
Dictionary in java java
The HashSet Class in java java
Rooted and ordered trees in data structure using java data structure
Square Root Binary Search in java Algorithm
Inheritance in java java

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Affiliate Marketing Principles
  • The Basics You Need to Know About Affiliate Marketing
  • Affiliate Marketing Options
  • All About Affiliate Marketing
  • Classification of Database Management Systems
  • Three-Tier and n-Tier Architectures
    for Web Applications
  • Two-Tier Client/Server Architectures for DBMSs
  • Basic Client/Server Architectures in DBMS
  • Centralized DBMSs Architecture in DBMS
  • Tools, Application Environments, and Communications Facilities in DBMS

Categories

  • Affiliate marketing (5)
  • Algorithm (43)
  • amp (3)
  • android (223)
  • Android App (8)
  • Android app review (4)
  • android tutorial (60)
  • Artificial intelligence (61)
  • AWS (3)
  • bitcoin (8)
  • blockchain (1)
  • c (5)
  • c language (105)
  • cloud computing (4)
  • coding (4)
  • coding app (4)
  • complex number (1)
  • Computer Graphics (66)
  • data compression (65)
  • data structure (188)
  • DBMS (44)
  • digital marketing (9)
  • distributed systems (11)
  • ffmpeg (26)
  • game (3)
  • html (6)
  • image processing (35)
  • Inequalities (1)
  • information (4)
  • java (212)
  • java network (1)
  • javascript (9)
  • kotlin (4)
  • leetcode (1)
  • math (21)
  • maven (1)
  • mysql (1)
  • Node.js (8)
  • operating system (109)
  • php (310)
  • Principle Of Mathematical Induction (1)
  • programming (6)
  • Python (4)
  • Python data structure (9)
  • React native (1)
  • React.js (22)
  • Redux (1)
  • seo (4)
  • set (12)
  • trigonometry (6)
  • vue.js (35)
  • XML (3)

sitemap

sitemap of videos

sitemap of webstories

sitemap of website

  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy
  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy

© codingtube.tech