简体   繁体   中英

How to find if a String in an ArrayList contains a digit in as a char

There is a HashMap Having id and name. Name has some constraints. The Strings which match the criteria should be added to an ArrayList .

i) First character should be small and the last character should be Capital.
ii) In name at least one digit should be there.

Example: ravi5raJ


public static ArrayList<String> getName(HashMap<Integer,String> map) {
    ArrayList<String> a1=new ArrayList<String>();

    for (Integer key: map.keySet()) {
        int size=map.get(key).length();
        String name= map.get(key);
        int count=0;
        if(name.substring(0, 1)== name.substring(0, 1).toLowerCase())
        {
            if(name.substring(size-1, size-1)==name.substring(size-1, size-1).toUpperCase())
            {
                for (int i = 0; i < size ; i++) {
                    //logic to check for int
                    a1.add(name);
                }
            }

        }
    }

    return a1;
}

我认为您可以使用简单的正则表达式测试所有内容:

if( name.matches( "[a-z].*?\\d.*[A-Z]" ) ){ ...

Its working perfectly now.

  public static ArrayList<String> getName(HashMap<Integer,String> map) {
            ArrayList<String> a1=new ArrayList<String>();

            for (Integer key: map.keySet()) {
                int size=map.get(key).length();
                String name= map.get(key);
                int count=0;
                if( name.matches( "[a-z].*?\\d.*[A-Z]" ) )
                    a1.add(name);

            }
            return a1;
        }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM