[Go to site: main page, start]

0% found this document useful (0 votes)
5 views4 pages

Java String Methods

The document provides a reference for Java String classes, including String, StringBuffer, and StringBuilder, detailing their methods and characteristics. String is immutable, while StringBuffer and StringBuilder are mutable, with StringBuffer being thread-safe and StringBuilder being faster but not thread-safe. A comparison summary highlights their mutability, thread safety, performance, and best use cases.
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)
5 views4 pages

Java String Methods

The document provides a reference for Java String classes, including String, StringBuffer, and StringBuilder, detailing their methods and characteristics. String is immutable, while StringBuffer and StringBuilder are mutable, with StringBuffer being thread-safe and StringBuilder being faster but not thread-safe. A comparison summary highlights their mutability, thread safety, performance, and best use cases.
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

Java String Classes — Methods Reference

String · StringBuffer · StringBuilder

1. String
Immutable sequence of characters. Once created, the value cannot be changed. Every modification creates a new
String object. Defined in [Link].

Method Signature Description

length() int length() Returns the number of characters.

charAt(i) char charAt(int i) Returns char at index i.

indexOf(s) int indexOf(String s) First index of s, or -1 if not found.

lastIndexOf(s) int lastIndexOf(String s) Last index of s, or -1 if not found.

substring(b) String substring(int b) Substring from index b to end.

substring(b,e) String substring(int b, int e) Substring from b (inclusive) to e (exclusive).

contains(s) boolean contains(CharSequence True if string contains s.


s)

startsWith(p) boolean startsWith(String p) True if string starts with prefix p.

endsWith(s) boolean endsWith(String s) True if string ends with suffix s.

equals(o) boolean equals(Object o) True if content is equal (case-sensitive).

equalsIgnoreCase(s) boolean Case-insensitive equality check.


equalsIgnoreCase(String s)

compareTo(s) int compareTo(String s) Lexicographic comparison; 0 if equal.

toUpperCase() String toUpperCase() Converts all chars to uppercase.

toLowerCase() String toLowerCase() Converts all chars to lowercase.

trim() String trim() Removes leading and trailing whitespace.

strip() String strip() Unicode-aware whitespace removal (Java


11+).

replace(o,n) String replace(char o, char n) Replaces all occurrences of char o with n.

replaceAll(r,s) String replaceAll(String r, Replaces matches of regex r with s.


String s)

split(r) String[] split(String r) Splits around matches of regex r.

toCharArray() char[] toCharArray() Converts string to a char array.

valueOf(x) static String valueOf(x) Returns string representation of x.


Method Signature Description

concat(s) String concat(String s) Concatenates s to end of this string.

isEmpty() boolean isEmpty() True if length() == 0.

isBlank() boolean isBlank() True if empty or only whitespace (Java 11+).

matches(r) boolean matches(String r) True if whole string matches regex r.

intern() String intern() Returns canonical representation from pool.

format(f,...) static String format(String f, Returns formatted string (like printf).


...)

join(d,e...) static String join(CharSeq d, Joins elements with delimiter d.


...)

repeat(n) String repeat(int n) Returns string repeated n times (Java 11+).

chars() IntStream chars() Returns stream of char values (Java 9+).

Quick Example
String s = "Hello, World!";
[Link]([Link]()); // 13
[Link]([Link]()); // HELLO, WORLD!
[Link]([Link](7)); // World!
[Link]([Link]("World", "Java")); // Hello, Java!

2. StringBuffer
Mutable, thread-safe sequence of characters. All methods are synchronized, making it safe for use in multi-threaded
environments. Slightly slower than StringBuilder due to synchronization overhead. Defined in
[Link].

Method Signature Description

append(x) StringBuffer append(x) Appends any type (String, int, char, etc.).

insert(off,x) StringBuffer insert(int off, Inserts x at position off.


x)

delete(s,e) StringBuffer delete(int s, int Deletes chars from s (incl.) to e (excl.).


e)

deleteCharAt(i) StringBuffer deleteCharAt(int Deletes character at index i.


i)

replace(s,e,str) StringBuffer replace(int s, Replaces s..e with str.


int e, String str)

reverse() StringBuffer reverse() Reverses the character sequence in place.

charAt(i) char charAt(int i) Returns char at index i.


Method Signature Description

setCharAt(i,c) void setCharAt(int i, char c) Sets char at index i to c.

indexOf(s) int indexOf(String s) First occurrence of s, or -1.

lastIndexOf(s) int lastIndexOf(String s) Last occurrence of s, or -1.

substring(s) String substring(int s) Returns substring from s to end.

substring(s,e) String substring(int s, int e) Returns substring from s to e.

length() int length() Current number of characters.

capacity() int capacity() Current allocated capacity.

ensureCapacity(c) void ensureCapacity(int c) Ensures at least c capacity.

trimToSize() void trimToSize() Trims capacity to current length.

toString() String toString() Converts buffer content to a String.

subSequence(s,e) CharSequence subSequence(int Returns sub-sequence from s to e.


s, int e)

Quick Example
StringBuffer sb = new StringBuffer("Hello");
[Link](", World"); // Hello, World
[Link](5, " Java"); // Hello Java, World
[Link](5, 10); // Hello, World
[Link](); // dlroW ,olleH
[Link]([Link]());

3. StringBuilder
Mutable, non-thread-safe sequence of characters. Introduced in Java 5 as a faster alternative to StringBuffer for
single-threaded use. API is identical to StringBuffer but without synchronization. Defined in
[Link].

Method Signature Description

append(x) StringBuilder append(x) Appends any type (String, int, char, etc.).

insert(off,x) StringBuilder insert(int off, Inserts x at position off.


x)

delete(s,e) StringBuilder delete(int s, Deletes chars from s (incl.) to e (excl.).


int e)

deleteCharAt(i) StringBuilder deleteCharAt(int Deletes character at index i.


i)

replace(s,e,str) StringBuilder replace(int s, Replaces s..e with str.


int e, String str)
Method Signature Description

reverse() StringBuilder reverse() Reverses the character sequence in place.

charAt(i) char charAt(int i) Returns char at index i.

setCharAt(i,c) void setCharAt(int i, char c) Sets char at index i to c.

indexOf(s) int indexOf(String s) First occurrence of s, or -1.

lastIndexOf(s) int lastIndexOf(String s) Last occurrence of s, or -1.

substring(s) String substring(int s) Returns substring from s to end.

substring(s,e) String substring(int s, int e) Returns substring from s to e.

length() int length() Current number of characters.

capacity() int capacity() Current allocated capacity.

ensureCapacity(c) void ensureCapacity(int c) Ensures at least c capacity.

trimToSize() void trimToSize() Trims capacity to current length.

toString() String toString() Converts builder content to a String.

subSequence(s,e) CharSequence subSequence(int Returns sub-sequence from s to e.


s, int e)

Quick Example
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 5; i++) {
[Link](i);
if (i < 5) [Link](", ");
}
[Link]([Link]()); // 1, 2, 3, 4, 5

Comparison Summary

Feature String StringBuffer StringBuilder

Mutability Immutable Mutable Mutable

Thread Safety Thread-safe* Thread-safe (synchronized) Not thread-safe

Performance Slow (new obj each change) Moderate Fast

Storage String Pool / Heap Heap Heap

Introduced JDK 1.0 JDK 1.0 JDK 1.5

Best Used For Fixed/constant text Multi-threaded mutation Single-threaded mutation

* String is effectively thread-safe because it is immutable — no synchronization needed.

You might also like