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

[9528 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 Example{ public static void main(String args[]) { String s1 = new String("I Love Solution"); 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 Solution String after replacing 'o' with 'e': I Leve Selutien

    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 Example{ public static void main(String args[]) { String s1 = new String("I Love World"); 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 World
      String after replacing 'love' with 'like': I Like World

  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 Example{ public static void main(String args[]) { String s1 = "We gives you solution to all your technical needs"; //replace all white spaces with (-) String s2 = s1.replaceAll("\\s", "-"); System.out.println(s2); } }

    Output:

    We-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 Example{ public static void main(String args[]) { String s1 = "We give you solution to all your technical needs"; //replace first white space with (-) String s2 = s1.replaceFirst("\\s", "-"); System.out.println(s2); } }

    Output:

    We-give you solution to all your technical needs

                 



Want to Clear Java Interview? Read Java Interview Notes prepared by Us


Are You Good enough in Java? Prove it!



Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Quiz For You:

Search Tags

    replace vs replaceall in java

    java replaceall vs replace

    replaceall vs replacefirst

    replacefirst vs replace in java

    java replace different characters