Difference between replace(), replaceAll() and replaceFirst() Method of Java String with Example

(429 Views)


In Java String there are three types of Replace method. They are -

  1. replace ()
  2. replaceAll ()
  3. replaceFirst ()
Difference between replace, replaceFirst and replaceAll Method of Java String with Example

All these methods are used to replace characters in your string with some characters as you wish. So What's the difference between replaceAll, replaceFirst and replace in Java String? Let us look at each of these methods in detail with examples:

  1. replace() Method:
  2. There are two replace() methods in Java, they are -

    1. public String replace(char oldChar,char newChar):
    2. This method returns a String resulting from replacing all occurances of oldChar argument in this string with the newChar.

      Example:

      public class SolutionFactory { public static void main(String args[]) { String s1 = new String("I Love SolutionFactory"); System.out.println("Original String is ': " + s1); System.out.println("String after replacing 'o' with 'e': " + s1.replace('o', 'e')); } }

      Output:

      Original String is ': I Love SolutionFactory String after replacing 'o' with 'e': I Leve SelutienFactery

    3. public String replace(CharSequence target, CharSequence replacement):
    4. This method Replaces each substring of this string that matches the literal target sequence specified in the method with the specified literal replacement sequence specified in the method. The replacement proceeds from the beginning of the string to the end, for example, replacing "ss" with "j" in the string "sss" will result in "js" rather than "sj".

      Example:

      public class SolutionFactory { public static void main(String args[]) { String s1 = new String("I Love SolutionFactory"); System.out.println("Original String is ': " + s1); System.out.println("String after replacing 'love' with 'like': " + s1.replace("Love", "Like")); } }

      Output:

      Original String is ': I Love SolutionFactory
      String after replacing 'love' with 'like': I Like SolutionFactory

  3. replaceAll() Method:
  4. The syntax for replaceAll() Method is-

    public String replaceAll(String regex, String replacement)
    This method replaces each substring of this string that matches the given regular expression with the given replacement String in parameter.

    Example:

    public class SolutionFactory{ public static void main(String args[]) { String s1 = "SolutionFactory gives you solution to all your technical needs"; //replace all white spaces with (-) String s2 = s1.replaceAll("\\s", "-"); System.out.println(s2); } }

    Output:

    SolutionFactory-gives-you-solution-to-all-your-technical-needs

  5. replaceFirst() Method:
  6. The syntax for replaceFirst() Method is-

    public String replaceFirst(String regex, String replacement)
    This method replaces the first substring of this string that matches the given regular expression with the given replacement.

    Example:

    public class SolutionFactory{ public static void main(String args[]) { String s1 = "SolutionFactory gives you solution to all your technical needs"; //replace first white space with (-) String s2 = s1.replaceFirst("\\s", "-"); System.out.println(s2); } }

    Output:

    SolutionFactory-gives you solution to all your technical needs

Solution Worked 5 UpvotesUpvote

        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search