Java - Write code to check if a string is Alphanumeric Or Underscore

Requirements

Write code to check if a string is Alphanumeric Or Underscore

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String srcString = "book2s.com";
        System.out.println(isAlphanumericOrUnderscore(srcString));
    }//ww  w . ja v  a  2s  . c o  m

    private static final String ALPHANUMERIC_UNDERSCORE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

    public static boolean isAlphanumericOrUnderscore(String srcString) {
        return (lastIndexOfAnyBut(srcString, ALPHANUMERIC_UNDERSCORE) == -1);
    }

    public static int lastIndexOfAnyBut(String srcString, String validString) {
        int result = -1;
        int srcLen = srcString.length();

        // walk backward to find if a char within srcString is in validString
        for (int i = srcLen - 1; i >= 0; i--) {
            // not found, stop it
            if (validString.indexOf(srcString.charAt(i)) == -1) {
                result = i;
                break;
            }

        }

        return result;
    }
}