Skip to main content

Posts

Showing posts with the label regex

Which replace function works with regex?

-- More than often we need to manipulate a string, substitute characters inside a string. In Java, the String class, there are a couple of methods that we can use to complete this task. 1. public String replace(char oldChar, char newChar)    This method "returns a new string resulting from replacing all occurrences of oldChar in this string with newChar."    Both oldChar and newChar are single char.    For example, String a = "This is a cat.";    String b = a.replace('c', 'r');    b has the value of "This is a rat.". 2. public String replace(CharSequence target, CharSequence replacement)

Which replace function works with regex?

More than often we need to manipulate a string, substitute characters inside a string. In Java, the String class, there are a couple of methods that we can use to complete this task. 1. public String replace(char oldChar, char newChar)    This method "returns a new string resulting from replacing all occurrences of oldChar in this string with newChar."    Both oldChar and newChar are single char.    For example, String a = "This is a cat.";    String b = a.replace('c', 'r');    b has the value of "This is a rat.". 2. public String replace(CharSequence target, CharSequence replacement)    This method "replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence."        String c = "This is a cat.";    String d = c.replace("ca", "rabbi");    d has the value of "This is a rabbit.".   ...