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