0% found this document useful (0 votes)
2 views12 pages

String Methods and String Buffer Class

The document provides an overview of the String class and StringBuffer class in Java, highlighting their creation, storage, and key methods. It explains that Strings are immutable and includes various methods such as length(), charAt(), and substring(), while StringBuffer is mutable and allows modifications directly to the original object. Additionally, it discusses the advantages and disadvantages of using StringBuffer, suggesting StringBuilder for performance optimization in single-threaded scenarios.

Uploaded by

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

String Methods and String Buffer Class

The document provides an overview of the String class and StringBuffer class in Java, highlighting their creation, storage, and key methods. It explains that Strings are immutable and includes various methods such as length(), charAt(), and substring(), while StringBuffer is mutable and allows modifications directly to the original object. Additionally, it discusses the advantages and disadvantages of using StringBuffer, suggesting StringBuilder for performance optimization in single-threaded scenarios.

Uploaded by

rafiqmohamed025
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

String methods and

String buffer class


String creation and Storage:
What is String in Java?
In Java, String is an object created using the string class that represents a
sequence of characters.
Every character is stored in UTF-16 encoding. String class contains many useful
methods for concatenation , comparison and manipulation.

There are two ways to create a String in Java:


1) Using String literal(String pool)
2) Using New keyword(heap memory)
)

STRING METHODS:

Java String class includes many useful methods some of the important String
methods include:
1) length()
2) charAt(int index)
3) substring(int beginIndex, int endIndex)
4) contains(CharSequence s)
5) replace(char oldChar, char newChar)
String class is immuatable meaning it cannot be changed, if a string of this
class is changed it will actually create a new object instead of modifying the
original string
1.Length:
Returns the number of characters in the string.
Syntax: stringName.length();
Ex: String str=“Hello world”;
int len = str.length();
System.out.println(len);
//Output: 11
2.charAt:
Returns the character at the given index.
Syntax: charAt(int index_number);
Ex: String str=“Hello world”;
Char charac= str.charAt(6);
System.out.println(charac);
//Output : w
3.Substring
Returns a substring from a specified start index and end index.
Syntax: String sub= str.substring(startIndex, endIndex);
Ex: String str=“Hello world”;
String sub = str.substring(0,5);
System.out.println(sub);
//Output: Hello

4.Contains:
Returns true if a string contains a specified sequence of characters.
Syntax: boolean variableName= str.contains(string_content);
Ex: String str=“Hello world”;
Boolean result = str.contains(“world”);
System.out.println(result);
//Output: true
5.Replace:
Replaces all occurrences of an old character by the given new character.
Syntax: replace(‘oldchar’ , ’newchar’);
Ex: Ex: String str=“Hello world”;
String replaced= str.replace(‘o’,’a’);
System.out.println(replaced);
//Output: Hella warld

6.Equals:
Returns true if two strings are equal/Checks if two strings are equal.
Syntax:str1.equals(str2)
Ex: String str1= “hello”;
String str2= “hello’;
System.out.println(str1.equals(str2));
//Output: true
Other String methods:
•toLowerCase()
•toUpperCase()
•trim()
•split(String regex)
•lastIndexOf(String str)
•startsWith(String prefix)
•endsWith(String suffix)
•equalsIgnoreCase(String anotherString)
•valueOf()
StringBuffer CLASS

What is StringBuffer class?


The StringBuffer class in Java is m.utable unlike the usual String class,
meaning modifications of the original string are not stored in a new string
object but the original string object itself.

StringBuffer class methods:


1. append
2. insert
3. delete
4. reverse
Example program:
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer str = new StringBuffer("Hello");
str.append(" World");
System.out.println(str);
}
}
Output:
Hello World
Append:
Appends/ Adds the given string(or) character to the end of the string.
Syntax:
Str.append(“String_content”);
Ex: StringBuffer str = new StringBuffer("Hello");
str.append(" World");
System.out.println(str); //Output: Hello World

Insert:
Inserts a given string to a specified position.
Syntax:
insert(index,String_content);
Ex: StringBuffer str = new StringBuffer("Hello World");
str.insert(6,“Beautiful ");
System.out.println(str); //Output: Hello Beautiful World
Delete:
Deletes the characters given from a specified start to a specified end.
Syntax: delete(startIndex, endIndex); Note: Inclusive start, Exclusive end.
Ex:StringBuffer str = new StringBuffer("Hello World");
str.delete(0,6);
System.out.println(str); // Output: World

Reverse:
Reverses the characters in a given string.
Syntax: String.reverse();
Ex:StringBuffer str = new StringBuffer("Hello World");
str.reverse()
System.out.println(str); // Output: dlroW olleH
Advantages of StringBuffer class;
Advantages:
1. Mutable: Original String can be changed
2. Synchronized: Concurrent threads are synced so no thread state corruption
3. Performance:StringBuffer is generally more efficient than using String
due to reduced memory overhead and fewer object creations.

Disadvantage:
Speed of operation is reduced due to synchronization.

We can use StringBuilder instead of StringBuffer for single threaded programs for speed
optimization.

You might also like