DEV Community

PONVEL M
PONVEL M

Posted on

Mastering Java String Methods – Real-Time Scenario Practice

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();
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

== checks reference
equals() checks value


4. Email Validation using contains()

if(email.contains("@") && email.contains(".")) {
    System.out.println("Valid Email");
}
Enter fullscreen mode Exit fullscreen mode

5. Converting String to Integer

int num = Integer.parseInt("1234");
Enter fullscreen mode Exit fullscreen mode

6. Removing Spaces

String str = "  Welcome to Java  ";
str = str.trim();
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

8. Reversing a String

String rev = "";
for(int i = str.length()-1; i>=0; i--) {
    rev += str.charAt(i);
}
Enter fullscreen mode Exit fullscreen mode

9. Palindrome Check

if(str.equals(rev)) {
    System.out.println("Palindrome");
}
Enter fullscreen mode Exit fullscreen mode

10. Removing Duplicate Characters

if(result.indexOf(ch) == -1) {
    result += ch;
}
Enter fullscreen mode Exit fullscreen mode

11. Extracting Data using substring()

String result = log.substring(log.indexOf(":") + 2);
Enter fullscreen mode Exit fullscreen mode

12. Splitting CSV Data

String[] arr = data.split(",");
Enter fullscreen mode Exit fullscreen mode

13. Replacing Words

str.replace("Java", "Spring Boot");
Enter fullscreen mode Exit fullscreen mode

14. Mobile Number Validation

mobile.matches("\\d{10}");
Enter fullscreen mode Exit fullscreen mode

15. Splitting Using Custom Delimiter

str.split("#");
Enter fullscreen mode Exit fullscreen mode

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() and split()

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)