Saturday, November 9, 2019

Java String Matches Event - Regular Aspect Tutorial

String Matching Example inward Java
String matches method inward Java tin laissez passer on the sack hold upward used to attempt out String against regular human face inward Java. String matches() method is i of the most convenient agency of checking if String matches to a regular human face inward Java or not. Quite oft nosotros postulate to write code which needs to cheque if String is numeric, Does String contains alphabets e.g. H5N1 to Z or a to Z, How to cheque if String contains a detail digit half-dozen times etc. There are together with then many cases inward Java programming where nosotros postulate regular expression. If y'all are familiar amongst Java's regular human face API than y'all must know most java.util.regex.Pattern together with java.util.regex.Matcher class, these course of report provides regular human face capability to Java API. matches method of String course of report truly delegate asking to these classes. Anyway regular expression tin laissez passer on the sack hold upward best larn past times seeing few examples together with this is what nosotros are going to create inward this Java String matches example tutorial.

String matches instance Java

In this String matches tutorial nosotros volition run into ii examples of matches business office of String course of report :

1) How to cheque if string contains whatever alphabetic characters or non inward both e.g. A-Z or a-Z
2) How to cheque if string contains whatever numeric digits e.g. digits from 1-9 or 0-9

In next instance of matching String using regular human face nosotros accept used ii regular human face meta characters e.g. dot (.) which matches whatever grapheme together with astrick or star (*) which matches whatever expose of times. By using .* we are effectively matching whatever grapheme coming whatever expose of fourth dimension inward rootage String inward Java.



/**
 * Java plan to demonstrate how to purpose utilization String matches method
 * to fit regular human face inward String.
 *
 * @author Javin
 */

public class StringMatchExample {

    public static void main(String args[]) {
        String[] alphabets = {"", "12345", "A12345", "12345B",
                                  "12345a" , "abcd" , "aa343"};
     
        for(String alphabet : alphabets) {
           System.out.println(" does " + alphabet +
             " contains alphabetic words : " + alphabet.matches(".*[A-Za-z].*"));

        }
     
        //checking if String contains digits or not
        String[] numbers = {"1234" , "+1234", "234a"};
        for(String expose : numbers) {
           System.out.println(" expose " + expose + " contains exclusively 1-9 digits : "
               + number.matches(".*[1-9].*"));
        }
     
     
    }
}

Output:
does  contains alphabetic words : false
does 12345 contains alphabetic words : false
does A12345 contains alphabetic words : true
does 12345B contains alphabetic words : true
does 12345a contains alphabetic words : true
does abcd contains alphabetic words : true
does aa343 contains alphabetic words : true
expose 1234 contains exclusively 1-9 digits : true
expose +1234 contains exclusively 1-9 digits : true
expose 234a contains exclusively 1-9 digits : true
String matches method inward Java tin laissez passer on the sack hold upward used to attempt out String against regular human face inward Jav Java String Matches Example - Regular Expression Tutorial
Though y'all tin laissez passer on the sack purpose Pattern together with Matcher course of report for regular human face inward Java. matches method of String is overnice shortcut together with y'all tin laissez passer on the sack rapidly cheque if a detail String matches to a regular human face or not. These String fit examples are simply tip of iceberg inward price of regular human face but it does exhibit how to purpose fit method of String inward Java.

Further Learning
Data Structures together with Algorithms: Deep Dive Using Java
How to compare ii String inward Java

No comments:

Post a Comment