Java String Methods – Complete Practical Guide with Examples
By Bhau Automation • Learn all Java String class methods with practical coding and examples (Hindi + English)
🎯 What You Will Learn in This Video
- What is the Java String class and how it works
- Most used String methods in real projects
- Practical exercises using String methods
- String manipulation, comparison, and concatenation
- Common String interview questions
- Live coding demo with outputs
💡 Tip: In Java, Strings are immutable. Every modification creates a new object in memory.
📘 What is a String in Java?
The String class in Java represents a sequence of characters. Strings are widely used for text data like names, messages, and input processing.
Strings in Java are immutable, meaning their values cannot be changed after creation. To modify them, Java creates a new String object.
public class StringExample {
public static void main(String[] args) {
String name = "Bhau Automation";
System.out.println("Welcome to " + name);
}
}
🔹 Commonly Used Java String Methods
| Method | Description | Example |
|---|---|---|
length() | Returns the number of characters in a string | str.length() |
charAt(index) | Returns character at specified index | str.charAt(2) |
concat() | Joins two strings | str1.concat(str2) |
equals() | Checks if two strings are equal | str1.equals(str2) |
equalsIgnoreCase() | Compares strings ignoring case | str1.equalsIgnoreCase(str2) |
substring() | Extracts part of the string | str.substring(2, 5) |
toLowerCase() | Converts all letters to lowercase | str.toLowerCase() |
toUpperCase() | Converts all letters to uppercase | str.toUpperCase() |
trim() | Removes leading and trailing spaces | str.trim() |
replace() | Replaces characters | str.replace("a","@") |
💻 Java String Methods Example Program
public class StringMethodsDemo {
public static void main(String[] args) {
String s = " Bhau Automation ";
System.out.println("Length: " + s.length());
System.out.println("Character at index 2: " + s.charAt(2));
System.out.println("Lowercase: " + s.toLowerCase());
System.out.println("Uppercase: " + s.toUpperCase());
System.out.println("Trimmed: " + s.trim());
System.out.println("Substring (5,10): " + s.substring(5,10));
System.out.println("Replace: " + s.replace("Automation", "Testing"));
System.out.println("Equals Ignore Case: " + s.equalsIgnoreCase("bhau automation"));
}
}
💡 Output
Length: 17 Character at index 2: a Lowercase: bhau automation Uppercase: BHAU AUTOMATION Trimmed: Bhau Automation Substring (5,10): Autom Replace: Bhau Testing Equals Ignore Case: true
💬 Common Interview Questions
- Why are Strings immutable in Java?
- What is the difference between == and equals() in Strings?
- How does StringBuilder differ from String?
- Can Strings be modified after creation?
- What is String Pool in Java?
🎥 Watch the Complete Video Tutorial
👉 Watch on YouTube: Java String Methods Explained Practically
🎓 Key Takeaways
- Strings are immutable in Java.
- String methods help in manipulation and comparison.
- Use StringBuilder or StringBuffer for mutable strings.
- Understand equals() and == differences for interviews.
- Mastering String methods is essential for Java developers.
⚡ Next Steps: Practice all String methods and create mini programs to improve your understanding.
🚀 Created with ❤️ by Bhau Automation