JAVA – string methods – 2

This the second post for Java string methods. In this post I will detail more functions and its functionalities. 

matches()

This method tests string against given regular expression. This method returns true only if this string matches the given regular expression.

Syntax:

regex  the regular expression.

Example:

Result:

regionMatches()

This method has two different formats with different inputs (pramters) which can be used to test if two string regions are equal. It returns true if the specified subregion of this string matches the specified region of the string argument.

Syntax:

OR

i  the starting offset of the subregion in this string.

str  the string argument.

o  the starting offset of the subregion in the string argument.

len  the number of characters to compare.

ign  if true, ignore case when comparing characters.

Example:

Result:

replace()

This method searches for a character and repace it with new character creating new string by replacing all occurrences of oldChar in this string with newChar.

Syntax:

oldChar  the old character to be replaced.

newChar  the new character.

Example:

Result:

replaceAll() 

This method replaces each substring of this string that matches the given regular expression with the given replacement.

Syntax:

regex  the regular expression to which this string is to be matched.

str  the string which would replace found expression.

Example:

Result:

replaceFirst()

This method replaces the first substring of this string that matches the given regular expression with the given replacement.

Syntax:

regex  the regular expression to which this string is to be matched.

replacement  the string which would replace found expression.

Example:

Result: