Introduction to the Java String API for regular expressions' evaluation

The Java String API provides some useful methods to evaluate regular expressions against a text represented by the String object. Let's list those methods from the String class:

Method Signature

Purpose

boolean matches(String regex)

Matches the given regular expression against the string that the method is invoked on and returns true/false, indicating whether the match is successful (true) or not (false).

String replaceAll(String regex, String replacement)

Replaces each substring of the subject string that matches the given regular expression with the replacement string and returns the new string with the replaced content.

String replaceFirst(String regex, String replacement)

This method does the same as the previous one with the exception that it replaces only the first substring of the subject string that matches the given regular expression with the replacement string and returns the new string with the replaced content.

String[] split(String regex)

Splits the subject string using the given regular expression into an array of substrings (example given ahead).

String[] split(String regex, int limit)

This overloaded method does the same as the previous one but there is an additional second parameter. The limit parameter controls the number of times regular expressions are applied for splitting.

 

For the complete reference of the String class, refer to https://docs.oracle.com/javase/8/docs/api/java/lang/String.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43