Introduction
Strings are one of the most commonly used data types in Java. In real-world applications like login systems, data processing, and validation, we frequently work with strings.
Today, I practiced Java String methods using real-time scenarios, which helped me understand how they are used in actual applications.
1. Handling Extra Spaces (trim)
When a user enters a password with extra spaces, login may fail.
String password = "Admin123 ";
password = password.trim();
trim() removes unwanted spaces from start and end.
2. Case-Insensitive Comparison
User input and database values may differ in case.
if(ui.equalsIgnoreCase(db)) {
System.out.println("Match");
}
equalsIgnoreCase() ignores case differences.
3. Difference Between == and equals()
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
== checks reference
equals() checks value
4. Email Validation using contains()
if(email.contains("@") && email.contains(".")) {
System.out.println("Valid Email");
}
5. Converting String to Integer
int num = Integer.parseInt("1234");
6. Removing Spaces
String str = " Welcome to Java ";
str = str.trim();
7. Counting Word Occurrences
String str = "Java is easy. Java is powerful.";
int count = 0, index = 0;
while((index = str.indexOf("Java", index)) != -1) {
count++;
index += 4;
}
8. Reversing a String
String rev = "";
for(int i = str.length()-1; i>=0; i--) {
rev += str.charAt(i);
}
9. Palindrome Check
if(str.equals(rev)) {
System.out.println("Palindrome");
}
10. Removing Duplicate Characters
if(result.indexOf(ch) == -1) {
result += ch;
}
11. Extracting Data using substring()
String result = log.substring(log.indexOf(":") + 2);
12. Splitting CSV Data
String[] arr = data.split(",");
13. Replacing Words
str.replace("Java", "Spring Boot");
14. Mobile Number Validation
mobile.matches("\\d{10}");
15. Splitting Using Custom Delimiter
str.split("#");
Key Methods Learned
- trim()
- equalsIgnoreCase()
- contains()
- indexOf()
- substring()
- split()
- replace()
- matches()
My Learning Experience
Today’s practice was very useful because I understood how Java String methods are used in real-time scenarios rather than just theory.
I learned how to:
- Handle user input errors (like extra spaces and case issues)
- Validate data such as email and mobile numbers
- Process and manipulate strings efficiently
- Extract and transform data using methods like
substring()andsplit()
The most interesting part for me was removing duplicate characters and counting word occurrences, because it improved my logical thinking.
Overall, this exercise helped me gain confidence in using String methods for real-world applications and interviews. I now feel more comfortable solving string-based problems and explaining them clearly.
Conclusion
Java String methods are powerful tools for handling real-time data. Practicing these scenarios improves both coding skills and problem-solving ability, which are essential for interviews and real-world development.
Thank you for reading!
Top comments (0)