Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
stringutils
unicode
digits
communitycreator

What is StringUtils.getDigits 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.

getDigits is a static method of the StringUtils class that checks for Unicode digits in a string and returns a new string that contains all the digits in the given string. If the given string does not contain any digits, an empty string will be returned.

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 String getDigits(final String str)

Parameters

  • str: the string to extract the digits from.

Return value

The method returns a new string that contains only the digits present in the given string.

Code

import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String s = "hello educative123. Hello 345,68, mic check";
        System.out.printf("The digit string of \"%s\" is \"%s\"", s, StringUtils.getDigits(s));
        System.out.println();

        s = "hello educative";
        System.out.printf("The digit string of \"%s\" is \"%s\"", s, StringUtils.getDigits(s));
    }
}

Output


The digit string of "hello educative123. Hello 345,68, mic check" is "12334568"
The digit string of "hello educative" is ""

Explanation

  • string: hello educative123. Hello 345,68, mic check

The method will return 12334568, extracting all the digits and forming a string while the order of occurrence of the digits is preserved.

  • string: hello educative

The method will return an empty string as there are no digits in the string.

RELATED TAGS

java
stringutils
unicode
digits
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