String methods – matches and regionMatches

In this post, we will discuss about the String methods – matches and regionMatches.

boolean matches(String regex):

It returns true if String matches the given regular expression.

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

It throws PatternSyntaxException – if the regular expression’s syntax is invalid.

Output:
true
false

 

regionMatches():

This method tests if 2 string are equal or not. It is implemented in 2 ways.

  1. boolean regionMatches(int toffset,String other,int ooffset,int len)
  2. boolean regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len)

boolean regionMatches(int toffset,String other,int ooffset,int len):

A substring of the String object is compared to a substring of the another string. The result is true if these substrings represent identical character sequences.

Output:
true
false
[/su_note]
Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Print Friendly, PDF & Email
String methods - split()
StringBuffer class in java
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. Sandeep says:

    The way of explanation for each and every concept is very good , but small suggestion from my end is like it’s better if we can have much clear explanation (writing comments for each and every line of code) about programs which we have as examples in each and every concept.

Leave a Reply

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