Logo Search packages:      
Sourcecode: libcommons-lang-java version File versions  Download package

static int org::apache::commons::lang::StringUtils::indexOfAny ( String  str,
String[]  searchStrs 
) [inline, static]

Find the first index of any of a set of potential substrings.

A null String will return -1. A null or zero length search array will return -1. A null search array entry will be ignored, but a search array containing "" will return 0 if str is not null. This method uses String#indexOf(String).

 StringUtils.indexOfAny(null, *)                     = -1
 StringUtils.indexOfAny(*, null)                     = -1
 StringUtils.indexOfAny(*, [])                       = -1
 StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"])   = 2
 StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"])   = 2
 StringUtils.indexOfAny("zzabyycdxx", ["mn","op"])   = -1
 StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
 StringUtils.indexOfAny("zzabyycdxx", [""])          = 0
 StringUtils.indexOfAny("", [""])                    = 0
 StringUtils.indexOfAny("", ["a"])                   = -1
 

Parameters:
str the String to check, may be null
searchStrs the Strings to search for, may be null
Returns:
the first index of any of the searchStrs in str, -1 if no match

Definition at line 1343 of file StringUtils.java.

                                                                  {
        if ((str == null) || (searchStrs == null)) {
            return -1;
        }
        int sz = searchStrs.length;

        // String's can't have a MAX_VALUEth index.
        int ret = Integer.MAX_VALUE;

        int tmp = 0;
        for (int i = 0; i < sz; i++) {
            String search = searchStrs[i];
            if (search == null) {
                continue;
            }
            tmp = str.indexOf(search);
            if (tmp == -1) {
                continue;
            }

            if (tmp < ret) {
                ret = tmp;
            }
        }

        return (ret == Integer.MAX_VALUE) ? -1 : ret;
    }


Generated by  Doxygen 1.6.0   Back to index