String Methods in Java

ZoneTechPro
By -
0

 


String Methods in Java



Java - String Methods
Java - String Methods


Introduction :

In Java, strings come with various built-in methods to manipulate and process text efficiently. This guide covers essential string methods, including trim(), toLowerCase(), toUpperCase(), startsWith(), endsWith(), replace(), and replaceFirst().


1. String Methods

trim() :

The trim() method removes all leading and trailing spaces from a string and returns a new trimmed string.


class Main {

    public static void main(String[] args) {

        String mobile = "   9876543210 ";

        System.out.println(mobile.trim());

    }

}


Output:

9876543210

toLowerCase() :

The toLowerCase() method converts all characters in a string to lowercase.


class Main {

    public static void main(String[] args) {

        String name = "HEllO, How ARE yOu?";

        System.out.println(name.toLowerCase());

    }

}


Output:

hello, how are you?


toUpperCase() :

The toUpperCase() method converts all characters in a string to uppercase.


class Main {

    public static void main(String[] args) {

        String name = "helLO, How aRe yoU?";

        System.out.println(name.toUpperCase());

    }

}


Output:


HELLO, HOW ARE YOU?

startsWith() :

The startsWith() method checks if a string starts with a specified substring and returns true or false.


class Main {

    public static void main(String[] args) {

        String url = "https://www.google.com/";

        System.out.println(url.startsWith("https://"));

    }

}


Output:


true

endsWith() :

The endsWith() method checks if a string ends with a specified substring and returns true or false.


class Main {

    public static void main(String[] args) {

        String gmailId = "rahul123@gmail.com";

        System.out.println(gmailId.endsWith("@gmail.com"));

    }

}


Output:

true


replace() :

The replace() method replaces all occurrences of a character or substring with a new character or substring.


class Main {

    public static void main(String[] args) {

        String sentence = "teh cat and teh dog";

        System.out.println(sentence.replace("teh", "the"));

    }

}


Output:

the cat and the dog


replaceFirst() :

The replaceFirst() method replaces only the first occurrence of a substring in a string.


class Main {

    public static void main(String[] args) {

        String sentence = "teh cat and teh dog";

        System.out.println(sentence.replaceFirst("teh", "the"));

    }

}



Output:

the cat and teh dog

Handling Characters in replaceFirst() :

The replaceFirst() method only works with strings, not characters. Attempting to pass a char will result in a compilation error.


class Main {

    public static void main(String[] args) {

        String word = "Java";

        System.out.println(word.replaceFirst("a", "i"));

    }

}


Output:

Jiva



Summary

  • trim() – Removes leading and trailing spaces from a string.
  • toLowerCase() – Converts all characters in a string to lowercase.
  • toUpperCase() – Converts all characters in a string to uppercase.
  • startsWith() – Checks if a string starts with a specific substring and returns true or false.
  • endsWith() – Checks if a string ends with a specific substring and returns true or false.
  • replace() – Replaces all occurrences of a specific character or substring with a new value.
  • replaceFirst() – Replaces only the first occurrence of a specific substring in a string.
  • replaceFirst() limitation – Works only with strings, not with char values.
  • String methods usage – Useful for text formatting, validation, and manipulation in Java applications.

Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!