0% found this document useful (0 votes)
12 views10 pages

Java StringBuffer Methods Explained

The document provides an overview of Java's StringBuffer and StringBuilder classes, detailing their methods for manipulating strings, such as length, capacity, and character manipulation. It also compares StringBuffer and StringBuilder, highlighting their differences in synchronization and performance. Additionally, it covers the StringTokenizer class for tokenizing strings and introduces regular expressions for pattern matching in strings.

Uploaded by

Rehan Hussain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Java StringBuffer Methods Explained

The document provides an overview of Java's StringBuffer and StringBuilder classes, detailing their methods for manipulating strings, such as length, capacity, and character manipulation. It also compares StringBuffer and StringBuilder, highlighting their differences in synchronization and performance. Additionally, it covers the StringTokenizer class for tokenizing strings and introduces regular expressions for pattern matching in strings.

Uploaded by

Rehan Hussain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DURGASOFT

------------
it returns number of characters present in StringBuffer.

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link]());//4
}
}

int capacity()
--------------
it returns max number of characters it can hold.

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link]());//4
[Link]([Link]());//4+16=20
}
}

void ensureCapacity(int capacity)


---------------------------------
it is used to increase the capacity of string buffer object

Ex:
---
import [Link].*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
261  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link]());//4
[Link]([Link]());//4+16=20
[Link](50);
[Link]([Link]());//50
}
}

void setLength(int length)


--------------------------
it sets the length of string buffer object and removes extra content.

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link]());//4
[Link]([Link]());//4+16=20
[Link](2);
[Link](sb);//ab
[Link]([Link]());//2
[Link]([Link]());//20
}
}

void trimToSize()
-----------------
it finalize the given string with number of characters existed

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
262  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link]());//4
[Link]([Link]());//4+16=20
[Link](2);
[Link](sb);//ab
[Link]([Link]());//2
[Link]([Link]());//20
[Link]();
[Link](sb);//ab
[Link]([Link]());//2
[Link]([Link]());//2
}
}

char charAt(int index)


----------------------
it returns character located at the given index value.

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcd");
[Link](sb);//abcd
[Link]([Link](0));//a
[Link]([Link](1));//b
[Link]([Link](2));//c
[Link]([Link](3));//d
}
}

void setCharAt(int index,char ch)


---------------------------------
it perform replacement operation on the given string buffer

Ex:
---
import [Link].*;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
263  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("welkome");
[Link](sb);//welkome
[Link](3,'c');
[Link](sb);//welcome
}
}

void deleteCharAt(int index)


----------------------------
it removes the character located at the given index

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("welkome");
[Link](sb);//welkome
[Link](3);
[Link](sb);//welome
}
}

StringBuffer append(object)
---------------------------
it appends i.e. adds the given object at the end of string buffer

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
[Link](sb);//
[Link]("welcome ");
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
264  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
[Link](sb);//welome
[Link]("python ");
[Link](sb);//welome python
[Link]("programming");
[Link](sb);//welome python programming
}
}

StringBuffer insert(int index,object)


-------------------------------------
it inserts the given object at the specified location.

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
[Link](sb);//
[Link]("welcome ");
[Link](sb);//welome
[Link]("python ");
[Link](sb);//welome python
[Link]("programming");
[Link](7," to");
[Link](sb);//welome to python programming
[Link](10," java and");
[Link](sb);//welome to java and python programming
}
}

StringBuffer delete(int sindex,int eindex)


------------------------------------------
it removes the characters from s index to ending index

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
265  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
StringBuffer sb = new StringBuffer();
[Link](sb);//
[Link]("welcome ");
[Link](sb);//welome
[Link]("python ");
[Link](sb);//welome python
[Link]("programming");
[Link](7," to");
[Link](sb);//welome to python programming
[Link](10," java and");
[Link](sb);//welome to java and python programming
[Link](15,26);
[Link](sb);//welome to java programming
}
}

StringBuffer reverse()
----------------------
it reverse the given string buffer's content

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer("abcdefgh");
[Link](sb);//abcdefgh
[Link]();
[Link](sb);//hgfedcba
}
}

[Link] constructors and methods


------------------------------------------------
it is exactly same as [Link], except the following differences

StringBuffer vs StringBuilder
------------------------------
StringBuffer
-----------
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
266  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
=> 1.0 version
=> synchronized
=> only one thread is allowed
=> sequential execution
=> increase application time
=> performance improved
=> thread safe
=> deprecated/outdated

StringBuilder
-------------
=> 1.5 version
=> non-synchronized
=> multiple threads are allowed
=> parallel execution
=> decreases application time
=> performance not that good
=> not thread safe
=> not deprecated/outdated

[Link] constructors and methods


--------------------------------------------------
it is an utility class existed in [Link] package and StringTokenizer is used to divide
the given string into tokens.

"java is very easy" ------> java, is, very, easy


"19-12-2002" -------------> 19, 12, 2002
"09:30:45" ---------------> 09, 30, 45

StringTokenizer st = new StringTokenizer(String)


StringTokenizer st = new StringTokenizer(String,delimiter)

int countTokens() -------> returns number of tokens


boolean hasMoreTokens() -> returns true if there is a token
String nextToken() ------> returns current token and transfer the control to next

Ex:
---
import [Link].*;
class Test
{
public static void main(String[] args)
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
267  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
String s = "java is very easy";
StringTokenizer st = new StringTokenizer(s);
[Link]([Link]());//4
while([Link]()){
[Link]([Link]());
}
}
}
output:
-------
java
is
very
easy

Ex2:
----
import [Link].*;
class Test
{
public static void main(String[] args)
{
String s = "13-1-2023";
StringTokenizer st = new StringTokenizer(s,"-");
[Link]([Link]());//3
while([Link]()){
[Link]([Link]());
}
}
}

output:
-------
3
13
1
2023

Ex3:
----
import [Link].*;
class Test
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
268  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
public static void main(String[] args)
{
String s = "09:30:34";
StringTokenizer st = new StringTokenizer(s,":");
[Link]([Link]());//3
while([Link]()){
[Link]([Link]());
}
}
}

output:
-------
3
09
30
34

Regular Expression and Applications


------------------------------------
a group of strings according to a particular format or pattern or template is called as
regular expression.

steps to prepare re object


--------------------------
1) import [Link].*;

[Link] -> it imports all the classes and interfaces from util pkg not sub-pkgs
[Link] --> it imports all the classes and interfaces from regex sub-pkg

2) pattern object (format)


3) matcher object (target)

mobile number validation:


-------------------------
123 invalid
1234567890 invalid
7386237319 valid
1386237319 invalid

format or re: [6-9][0-9]{9}

Ex:
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
269  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
---
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
Pattern p = [Link]("[0-9]");//re
Matcher m = [Link]("a1b2c9d57u");//target string
int c=0;
while([Link]()){
[Link]([Link]()+" ====> "+[Link]()+" ====>
"+[Link]());
c++;
}
[Link]("count="+c);
}
}

1 ====> 2 ====> 1
3 ====> 4 ====> 2
5 ====> 6 ====> 9
7 ====> 8 ====> 5
8 ====> 9 ====> 7
count=5

Ex:
---
import [Link].*;
import [Link].*;
class Test
{
public static void main(String[] args)
{
Pattern p = [Link]("[a-z]");//re
Matcher m = [Link]("a1b2c9d57u");//target string
int c=0;
while([Link]()){
[Link]([Link]()+" ====> "+[Link]()+" ====>
"+[Link]());
c++;
}
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
270  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]

You might also like