Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    All about .replaceAll() in Java

    Blogs
    java string manipulation
    4
    4
    663
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Udith Indrakantha
      Udith Indrakantha Web Development last edited by Udith Indrakantha

      alt text
      java.lang.String class brings you a very powerful method to do replacements within a string.

      This method allows you to target specific substrings using regular expression ,so that you can even remove and replace any pattern within a string.

      Mainly, this method can

           * replace a given substring found any where in the string
           * replace a given character found any where in the string
           * replace a given reg expression found any where in the string
      

      Even though there are separate methods like replace(char old, char new), replace(charSequence old, charSequence new) to do above functions, this replaceAll() method acts as a versatile method for all those functions.

      This is how this method is defined in String class,

      String replaceAll(String regex , String new)

      As you can see , this method returns a new string. Actually, a string is immutable in JAVA , so it is not possible to alter a string. This method returns a new string instead.You need to catch it with a string variable.

      **This method accepts to two parameters which must be in type String. So , when you need to replace a character, you have to use a double quotation for the characters (instead of single quotations which are used for representing char).

      Examples:

      alt text
      Character replacement

      alt text
      String replacement

      alt text
      Regex replacement

      Keep in Touch!!!

      1 Reply Last reply Reply Quote 2
      • root
        root Linux Help last edited by

        keep it up. awesome

        1 Reply Last reply Reply Quote 1
        • dev_lak
          dev_lak last edited by

          awesome bro

          1 Reply Last reply Reply Quote 1
          • Nubelle
            Nubelle Web Development last edited by

            good work bro

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post

            0
            Online

            3.7k
            Users

            1.3k
            Topics

            5.3k
            Posts

            • Privacy
            • Terms & Conditions
            • Donate

            © Copyrights and All right reserved Lanka Developers Community

            Powered by Axis Technologies (PVT) Ltd

            Made with in Sri Lanka

            | |