[Go to site: main page, start]

0% found this document useful (0 votes)
3 views3 pages

Java String StringBuffer Operations

The document outlines operations for Java String and StringBuffer classes. It details various String operations including creation, length, character access, substring, concatenation, comparison, searching, case conversion, trimming, replacing, splitting, joining, value conversion, formatting, and repeating. Additionally, it covers StringBuffer operations such as creation, appending, inserting, replacing, deleting, reversing, and managing capacity and length.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java String StringBuffer Operations

The document outlines operations for Java String and StringBuffer classes. It details various String operations including creation, length, character access, substring, concatenation, comparison, searching, case conversion, trimming, replacing, splitting, joining, value conversion, formatting, and repeating. Additionally, it covers StringBuffer operations such as creation, appending, inserting, replacing, deleting, reversing, and managing capacity and length.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java String and StringBuffer Operations

1. String Operations
In Java, String is immutable and belongs to [Link] class. Various operations can be
performed on String objects.

Creation

String s1 = "Hello";
String s2 = new String("World");

Length

String s = "Java Programming";


[Link](); // 16

Character Access

[Link](5); // returns P

Substring

[Link](5); // "Programming"
[Link](0,4); // "Java"

Concatenation

s1 + s2; or [Link](s2);

Comparison

[Link](s2);
[Link](s2);
[Link](s2);

Searching

[Link]("Java");
[Link]("Pro");// return the index number
[Link]("Ja");
[Link]("ing");

Case Conversion

[Link](); // return the updated string


[Link]();

Trimming

" Hello ".trim(); // "Hello"

Replace

"I like Java java java ".replace("Java",


"Python"); //.replaceALL(old,new), .replaceFirst()

Splitting

"red green blue blue".split("");

Joining

[Link]("-", "2025", "08", "22"); // 2025-08-22

Value Conversion

[Link](123); // "123"

Formatting

[Link]("Name: %s, Age: %d", "Alice", 25);

Repeat (Java 11+)

"Hi ".repeat(3); // Hi Hi Hi
2. StringBuffer Operations
StringBuffer is mutable and belongs to [Link] class. It is thread-safe and
allows modification of strings.

Creation

StringBuffer sb1 = new StringBuffer();


StringBuffer sb2 = new StringBuffer("Hello");

Append

[Link](" World"); // Hello World

Insert

[Link](5, " Java"); // Hello JavaWorld

Replace

[Link](6, 10, "Python");

Delete

[Link](3, 7);

Reverse

[Link]();

Capacity and Length

[Link]();
[Link]();

Substring

[Link](0, 5);

You might also like