Skip to content

StringUtils.containsOnly

Signature: StringUtils.containsOnly(inputString, searchChars)

Returns: Boolean result - true if the inputString contains only characters in the searchChars, false if not or null inputString

Checks if inputString contains only characters in the searchChars, handling null. A null inputString will return false.

Parameters

Type Name Description Default
String inputString the String to check, may be null
String or char[] searchChars the characters to find, may be null

Example

// Examples of how to use StringUtils.containsOnly
var text = "Do I contain what you are looking for?";


// Example: searchChars as String
if (StringUtils.containsOnly(
       text,
       "oIconDtaiwhyulkgfre? "
    )) {
   // Every character in the text appears in the allowed set. A narrower set such
   // as "abc" would return false.
}


// Example: searchChars as char[]
if (StringUtils.containsOnly(
       text,
       ['D', 'o', ' ', 'I', 'c', 'n', 't', 'a', 'i', 'w', 'h', 'y', 'u', 'l', 'k', 'g', 'f', 'r', 'e', '?']
    )) {
   // Same check with the allowed characters supplied as an array.
}