Minggu, 08 Oktober 2017

Java Regular Expression

Some useful java function that may help me in the java project
  • Convert List of integer into any literal separated string
  • public static String joinList(List<Integer> list, String literal) {
    return list.toString().replaceAll(",", literal).replaceAll("[\\[.\\].\\s+]", "");
    }
  • Generate random number of 10 digits
  • public static String randomNumber(){
    Long randomNumber = (long) Math.floor(Math.random() * 9000000000L) + 1000000000L;
    String number = Long.toString(randomNumber);
    return number;
    }
  • Get Current date into the specified format
  • public static String currentDate(String dateFormat){
    SimpleDateFormat s = new SimpleDateFormat(dateFormat);
    Calendar cal = Calendar.getInstance();
    return s.format(new Date(cal.getTimeInMillis()));
    }
  • Check given input string is numeric or not
  • public static boolean isNumeric(String inputString) {
    return inputString.matches("[-+]?\\d*\\.?\\d+");
    }
  • Check given input string is alphabetic or not
  • public static boolean isAlphabetic(String inputString) {
    return inputString.matches("[a-zA-Z]+");
    }
  • Regular expression to validate email address
  • public static boolean isEmailFormat(String inputString) {
    return inputString.matches("^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$");
    }
  • Regular expression to validate alphanumeric string
  • public static boolean isAlphaNumeric(String inputString) {
    return inputString.matches("^[a-zA-Z0-9\\s]*$");
    }

Tidak ada komentar:

Posting Komentar