Java String Methods (Advanced)

Java String Methods (Advanced)

Java provides many built-in methods to work with strings. These methods allow searching, modifying, comparing, trimming, splitting, and formatting string values.

Below are the most commonly used advanced string methods with examples.


🔹 1. charAt()

Returns a character at a specific index.

String text = "Hello";
System.out.println(text.charAt(1)); // Output: e

🔹 2. substring()

Extracts a part of the string.

String text = "Hello Java";
System.out.println(text.substring(6)); // Java
System.out.println(text.substring(0, 5)); // Hello

🔹 3. contains()

Checks whether a string contains a sequence of characters.

String s = "Java Programming";
System.out.println(s.contains("Java")); // true
System.out.println(s.contains("Python")); // false

🔹 4. startsWith() and endsWith()

String text = "Programming";

System.out.println(text.startsWith("Pro")); // true
System.out.println(text.endsWith("ing")); // true


🔹 5. replace() and replaceAll()

String text = "Java is fun";
System.out.println(text.replace("fun", "awesome")); // Java is awesome

replaceAll() is used for regex patterns:

String text = "A1B2C3";
System.out.println(text.replaceAll("[0-9]", "")); // ABC

🔹 6. trim()

Removes leading and trailing spaces.

String text = " Hello World ";
System.out.println(text.trim()); // "Hello World"

🔹 7. split()

Splits the string into an array.

String data = "apple,banana,orange";
String[] fruits = data.split(",");

for(String f : fruits) {
System.out.println(f);
}


🔹 8. toCharArray()

Converts string into an array of characters.

String text = "ABC";
char[] arr = text.toCharArray();

for(char c : arr){
System.out.println(c);
}


🔹 9. compareTo()

Compares two strings lexicographically.

System.out.println("apple".compareTo("banana")); // negative
System.out.println("banana".compareTo("apple")); // positive
System.out.println("apple".compareTo("apple")); // 0

🔹 10. format()

Formats the string like printf.

String result = String.format("Name: %s, Age: %d", "Vipul", 22);
System.out.println(result);

🔹 11. repeat() (Java 11+)

Repeats a string multiple times.

String text = "Hi ";
System.out.println(text.repeat(3)); // Hi Hi Hi

🔹 12. isEmpty() and isBlank() (Java 11+)

String s1 = "";
String s2 = " ";

System.out.println(s1.isEmpty()); // true
System.out.println(s2.isBlank()); // true (checks whitespace)


⭐ Full Example Using Multiple Methods

public class Main {
public static void main(String[] args) {
String text = " Java Programming ";

System.out.println("Original: " + text);
System.out.println("Trimmed: " + text.trim());
System.out.println("Upper: " + text.toUpperCase());
System.out.println("Contains 'Java'? " + text.contains("Java"));
System.out.println("Substring: " + text.substring(2, 6));
System.out.println("Char At index 3: " + text.charAt(3));
System.out.println("Repeat: " + text.trim().repeat(2));
}
}


📌 Summary

Method Purpose
charAt(), toCharArray() Character access
substring() Extract part of string
replace(), replaceAll() Modify text
contains(), startsWith(), endsWith() Search in strings
trim(), isEmpty(), isBlank() Clean text
split() Break string
compareTo() Compare strings
format(), repeat() Formatting & repetition

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *