How to Replace Dot in Java?

How to Replace Dot in Java? In Java programming language, to replace a dot in a string we can use either replace() or replaceAll() method. Both of these methods are built-in methods in the Java String class.

Replace Dot in Java using replace() Method

There are 2 forms of the replace() method in String class:-

  • public String replace(char oldChar, char newChar)
  • public String replace(CharSequence target, CharSequence replacement)

The first replace() method takes two string parameters, and the first parameter is replaced by the second parameter. But to solve our problem second form of the replace() method is useful.

Method Syntax:- public String replace(CharSequence target, CharSequence replacement)

  • Parameters: It takes two parameters.
    • target – The sequence of char values to be replaced.
    • replacement – The replacement sequence of char values.
  • Return: A replaced string.

Program to Replace dot in Java using replace() Method

public class Main {
   public static void main(String args[]) {
      String str = "www.knowprogram.com";
      System.out.println(str.replace(".", " "));
   }
}

Output:-

www knowprogram com

Replace Dot in Java using the replaceAll() method

Instead of the replace() method, we can also use the replaceAll() method to replace the dot in Java. The replaceAll() also works very similarly to the replace() method. See more:- Java replace() vs replaceAll() Method

Method Syntax:- public String replaceAll(String regex, String replacement)

  • Parameters: It takes 2 parameters.
    • regex – the regular expression to which this string is to be matched.
    • replacement – the string to be substituted for each match.
  • Return: A replaced string.
  • Throws: PatternSyntaxException, if the regular expression’s syntax is invalid

The regex is a regular expression that will be placed instead of the substring, & replacement is a substring that needs to be replaced by regex. Let us see an example of how the replace() method works:-

String str = "Know mrogram";
System.out.println(str.replaceAll("m", "P"));

Output:- Know PrograP

The above code snippet replaces all the occurrences of ‘m’ by ‘P’. This example replaces a normal character with some other character. By using replace() method, not only the character but also strings can be replaced. As we know replaceAll() method is also given in the string class which returns a new string after replacing all the matched characters. It also replaces special characters like “!@#$%^&*()” but to replace a dot “.” character is a bit tedious task. 

In Java, the dot character matches all the characters by default hence to replace a dot character explicitly we use backslash “\”. The backslash character is an escape character using it with the dot “.” might escape the dot “.” character also. Hence we use double backslash character to select the “.” dot character explicitly. Observe the below code. 

Program to Replace dot in Java using replaceAll() Method

public class Main {
   public static void main(String args[]) {
      String str = "www.knowprogram.com";
      System.out.println(str.replaceAll("\\.", " "));
   }
}

Output:-

www knowprogram com

The above program replaces all the dot ”.” character by space “ “.  The replaceAll method works the same as “Pattern.compile(regex).matcher(str).replaceAll(repl)”  Pattern.compile() method in the regex class. Also See:- Swap Characters in String Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *