Friday, 22 April 2016

What are the regex methods of the String class ?


Regex methods of the String class

myString.matches("regex")
It returns true or false, depending whether the string can be matched entirely by the regular expression.
String.matches() only returns true if the entire string can be matched.
If myString is abc then myString.matches("bc") returns false.

myString.replaceAll("regex", "replacement")
Replaces all regex matches inside the string with the replacement string you specified.
No surprises here. All parts of the string that match the regex are replaced.

myString.split("regex")
Splits the string at each regex match.
The method returns an array of strings where each element is a part of the original string between two regex matches.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.