A string is a sequence of characters that is stored as a char array and is represented by the String class. Strings are commonly used to store and display text such as names, addresses, and other kinds of information. They are also used to store and manipulate data read from external sources, such as text files or user input.
Strings are also widely used in Java for formatting and parsing data. For example, you can use the String.format method to format numbers and dates as strings, or use the parseInt and parseDouble methods to convert strings to numbers.
To know more about String Class in java read the complete article.
String matches() method in Java.
The Java String matches() method is a static method of the java.lang.String class that is used to check if a given string matches a specified regular expression. This method returns a boolean value indicating whether the string matches the regular expression.
The String.prototype.startsWith() method determines whether a string begins with the characters of a specified string. This method returns a boolean value indicating whether the original string starts with the characters of the specified string.
The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
Returns True if, and only if, this string matches the given regular expression
Throws: PatternSyntaxException - if the regular expression's syntax is invalid
regex - the regular expression to which this string is to be matched
Let’s look at a string-related Java program where the Java String startsWith() Method is used to perform an operation on the given string.
Example: Java String Matches Method
public class Main {
public static void main(String args[]) {
String Str = new String("PrepInsta is a learning Platform");
System.out.print("Return Value :" );
System.out.println(Str.matches("(.*)Platform(.*)"));
System.out.print("Return Value :" );
System.out.println(Str.matches("Platform"));
System.out.print("Return Value :" );
System.out.println(Str.matches("PrepInsta(.*)"));
}
}
Output
true
false
false
Explanation:Here the output comes the matches method is used to check if a string matches a specific string.
public class Main {
public static void main(String args[]) {
String str = "Hello, World!";
System.out.println(str.matches("Hello, World!"));
System.out.println(str.matches("^H.*d!$"));
System.out.println(str.matches("^h.*d!$"));
}
}
Output
true
true
false
Explanation:Here, the output delivers the matches method checks if the string is exactly equal to the specified string. In the second example, the matches method checks if the string starts with an "H", ends with a "d!", and has any number of characters in between. In the third example, the matches method checks if the string starts with an "h" (lowercase) and returns false because the string starts with an "H" (uppercase).
Example 3: Java Spilt Method Using Regex and Length
public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
// Check if the string matches a specific string
System.out.println(str.matches("Hello, World!")); // true
// Check if the string matches a regular expression
System.out.println(str.matches("^H.*d!$")); // true
System.out.println(str.matches("^h.*d!$")); // false
// Check if the string is a valid email address
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
String email = "[email protected]";
System.out.println(email.matches(emailRegex)); // true
// Check if the string is a valid phone number
String phoneRegex = "^\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})$";
String phone = "123-456-7890";
System.out.println(phone.matches(phoneRegex)); // true
}
}
Output
true
true
false
true
true
Explanation:In this example, the matches method is used to check if a string matches a specific string, a regular expression, a valid email address, and a valid phone number. The matches method returns a boolean value indicating whether the string matches the specified pattern.
If you need to perform a case-insensitive match, you can use the (?i) flag in the regular expression to ignore the case of the characters.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment