What is the StringUtils.isMixedCase in Java?

Overview

isMixedCase() is a static method of the StringUtils class that is used to check if the given string contains both uppercase and lowercase characters.

  • The method returns false if the input string is null.
  • The method returns false if the input string is empty.

How to import StringUtils?

StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.


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

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

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax


public static boolean isMixedCase(final CharSequence cs)

Parameters

  • final CharSequence cs: the character sequence/string to be checked

Return value

This method returns true if the string contains both uppercase and lowercase characters. Otherwise, it returns false.

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "educATIve";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "educ ATIve";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "23asfg";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = " ";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "\u002f\u0045";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "educative";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
s = "EDUCATIVE";
System.out.printf("The output of StringUtils.isMixedCase() for the string - '%s' is %s", s, StringUtils.isMixedCase(s));
System.out.println();
}
}

Example 1

  • string - "educATIve"

The method returns true because the string contains both uppercase and lowercase characters.

Example 2

  • string - "educ ATIve"

The method returns true because the string contains both uppercase and lowercase characters.

Example 3

  • string - "23asfg"

The method returns false because the string only contains lowercase characters and numbers.

Example 4

  • string - ""

The method returns false because the string is empty.

Example 5

  • string - null

The method returns false because the string points to null reference.

Example 6

  • string - " "

The method returns false because the string contains only spaces that are neither uppercase nor lowercase characters.

Example 7

  • string - "\u002f\u0045"

The method returns false because the string contains Unicode letters which are neither uppercase nor lowercase characters.

Example 8

  • string - "educative"

The method returns false because the string contains lowercase characters only.

Example 9

  • string - "EDUCATIVE"

The method returns false because the string contains uppercase characters only.

Output

The output of the code will be as follows:


The output of StringUtils.isMixedCase() for the string - 'educATIve' is true
The output of StringUtils.isMixedCase() for the string - 'educ ATIve' is true
The output of StringUtils.isMixedCase() for the string - '23asfg' is false
The output of StringUtils.isMixedCase() for the string - '' is false
The output of StringUtils.isMixedCase() for the string - 'null' is false
The output of StringUtils.isMixedCase() for the string - '  ' is false
The output of StringUtils.isMixedCase() for the string - '/E' is false
The output of StringUtils.isMixedCase() for the string - 'educative' is false
The output of StringUtils.isMixedCase() for the string - 'EDUCATIVE' is false

Free Resources