DEV Community

KIRAN RAJ
KIRAN RAJ

Posted on Edited on

String Methods Based Programs

1. Remove trailing space (password issue)

The trim() method removes unwanted spaces from the beginning and end of a string. It is commonly used to clean user input.

String pwd = " Admin123 ";
pwd = pwd.trim(); 
System.out.println(pwd);
Enter fullscreen mode Exit fullscreen mode

2. Compare username safely (ignore case)

The equalsIgnoreCase() method compares two strings while ignoring case differences. It is useful for username validation.

String s1 = "Vijay";
String s2 = "vijay";
System.out.println(s1.equalsIgnoreCase(s2));
Enter fullscreen mode Exit fullscreen mode

3. == vs equals()

== compares memory references, whereas equals() compares actual values. This is a very important concept for interviews.

String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);        // false (memory check) 
System.out.println(s1.equals(s2));   // true (value check)
Enter fullscreen mode Exit fullscreen mode

4. Check email contains '@' and '.'

The contains() method checks whether a string contains a specific substring. It is useful in validations like email checking.

String email = "test@gmail.com";
if(email.contains("@") && email.contains(".")) {        System.out.println("Valid");
}
else {
 System.out.println("invalid");
}
Enter fullscreen mode Exit fullscreen mode

5. Convert String to int

The Integer.parseInt() method converts a string into an integer. It is commonly used in form input processing.

String num = "12345";
int n = Integer.parseInt(num);
System.out.println(n+20);
Enter fullscreen mode Exit fullscreen mode

6. Remove spaces only start & end

The split() method divides a string into multiple parts based on a delimiter. It is widely used in CSV parsing and data processing.

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

7. Count "Java" occurrences

The length() method returns the length of a string. It is very useful when working with loops.

String str = "Java is easy. Java is powerful.";
int count = 0;

String[] words = str.split(" ");
for(String w : words){
    if(w.equals("Java")) count++;
    }
System.out.println(count);
Enter fullscreen mode Exit fullscreen mode

8. Reverse string (without StringBuilder)

The charAt() method returns the character at a specific index. It is used for string traversal.

String str = "Java";
String rev = "";

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

## 9. Check palindrome

The indexOf() method returns the position of a character or substring. It is useful for checking duplicates.

String str = "madam";
String rev = "";

for(int i = str.length()-1; i >= 0; i--){
   rev += str.charAt(i);
}

if(str.equals(rev))
    System.out.println("Palindrome:"+rev);
else
 System.out.println("Not Palindrome");
Enter fullscreen mode Exit fullscreen mode

10. Remove duplicate characters

The equals() method compares the actual content of two strings. It is commonly used in palindrome checks.

String str = "aabbccdd";
String result = "";

for(int i=0; i<str.length(); i++){
    char c = str.charAt(i);
    if(result.indexOf(c) == -1){
       result += c;
    }
}
System.out.println(result);
Enter fullscreen mode Exit fullscreen mode

11. Extract message from log

The replace() method replaces one part of a string with another. It is useful for modifying text.

String log = "ERROR: File not found";

String result = log.split(":")[1].trim();
System.out.println(result);
Enter fullscreen mode Exit fullscreen mode

12. CSV extract

The matches() method uses regular expressions to validate patterns. It is commonly used for mobile number or password validation.

String data = "Vijay,25,Chennai";
String[] arr = data.split(",");

System.out.println("Name: " + arr[0]);
System.out.println("Age: " + arr[1]);
System.out.println("City: " + arr[2]);
Enter fullscreen mode Exit fullscreen mode

13. Replace word

The + operator is used to combine strings. It is often used in building new strings like in reverse logic.

String str = "I love Java programming";
str = str.replace("Java", "Spring Boot");

System.out.println(str);
Enter fullscreen mode Exit fullscreen mode

14. Validate mobile number (10 digits)

After using split(), array indexing is used to access individual values.

String num = "9876543210";

if(num.matches("\\d{10}")){
   System.out.println("Valid");
}else{
   System.out.println("Invalid");
}
Enter fullscreen mode Exit fullscreen mode

15. Split string

Loops are used to traverse strings. This is the base for solving most string problems.

String str = "apple#banana#mango";

String[] arr = str.split("#");

for(String s : arr){
    System.out.println(s);
}   
Enter fullscreen mode Exit fullscreen mode

Top comments (0)