Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

string
java
communitycreator

What is StringUtils.containsNone() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The containsNone() method is a static method of the StringUtils class. It is used to check that the given string does not contain certain charactersinvalid characters.

StringUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven project, add the following dependency to pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

Note: For other versions of the commons-lang package, refer to the Maven Repository.

StringUtils class can be imported as follows:

import org.apache.commons.lang3.StringUtils;

Example 1

invalid_characters = "tvs"   
string = "ababaaaabbbcccac"

As this string doesn’t contain any characters that are invalid characters, applying this function would return true.

Example 2

invalid_characters = "tvs"   
string = "aba**v**adefaabb**s**cccac**t**"

As this string contains characters that are invalid characters, applying this function would return false.

Syntax

public static boolean containsNone(final CharSequence cs, final char... searchChars)

Parameters

  • final CharSequence cs: Character Sequence to check.
  • final char... searchChars: An array of invalid characters.

Overloaded Method

public static boolean containsNone(final CharSequence cs, final String invalidChars)

Parameters

  • final CharSequence cs: Character Sequence to check
  • final String invalidChars: A string of invalid characters

Returns

It returns true if the character sequence does not contain any passed invalid characters and is not null. Otherwise it returns false.

Example

In the code below, we define an array of invalid characters and a sequence of characters. The output of the code will be as follows:

true
false

The first sequence of characters does not contain any invalid characters and the second sequence of characters contains invalid characters that are defined in the array of invalid characters.

import org.apache.commons.lang3.StringUtils;

public class Main{

    public static void main(String[] args){
        String characterSequence = "abbbcccaaaccb";
        char[] validChars = "tsv".toCharArray();
        System.out.println(StringUtils.containsNone(characterSequence, validChars));

        characterSequence = "atbsljkhbv";
        System.out.println(StringUtils.containsNone(characterSequence, validChars));
    }
}

RELATED TAGS

string
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring