Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
stringutils

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

toCodePoints() is a staticdescribes the methods in Java that can be called without creating an object of the class method of the StringUtils class which is used to convert the given character sequence/string to an array of code points.

Note: Code points are numbers that identify a symbol.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add 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 int[] toCodePoints(final CharSequence cs)

Parameters

final CharSequence cs: The character sequence or string to convert.

Return value

This method returns an integer array of code points.

Code

import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String s = "HellO-EDUcative";
System.out.printf("The output of StringUtils.toCodePoints() for the string - '%s' is %s", s, Arrays.toString(StringUtils.toCodePoints(s)));
System.out.println();
s = "";
System.out.printf("The output of StringUtils.toCodePoints() for the string - '%s' is %s", s, Arrays.toString(StringUtils.toCodePoints(s)));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.toCodePoints() for the string - '%s' is %s", s, Arrays.toString(StringUtils.toCodePoints(s)));
System.out.println();
}
}

Example 1

  • string = "HellO-EDUcative"

The method returns an array of code points: [72, 101, 108, 108, 79, 45, 69, 68, 85, 99, 97, 116, 105, 118, 101].

Example 2

  • string = ""

The method returns [] because the input string is empty.

Example 3

  • string = null

The method returns null because the input string is null.

Output

The output of the code is as follows:


The output of StringUtils.toCodePoints() for the string - 'HellO-EDUcative' is [72, 101, 108, 108, 79, 45, 69, 68, 85, 99, 97, 116, 105, 118, 101]
The output of StringUtils.toCodePoints() for the string - '' is []
The output of StringUtils.toCodePoints() for the string - 'null' is null

RELATED TAGS

java
communitycreator
stringutils

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