[Java] Get the length of the surrogate pair string

What is a surrogate pair?

In Unicode (UTF-16), one character is usually represented by two bytes. However, as the number of characters that should be handled by Unicode increased, the number of characters that can be expressed in 2 bytes (65535 characters) became insufficient, and by expressing some characters in 4 bytes, the number of characters that can be handled increased. .. Such 4-byte characters are called surrogate pairs.

Get the length of the surrogate pair string

The character "rebuke" is a surrogate pair, so if you normally use the length method, it will be considered two characters. Therefore, to correctly count strings containing surrogate pairs, use the codePointCount method instead of the length method.

var str1 = "Hello";
System.out.println(str1.length());   //Result: 5

var str2 = "Scold";
System.out.println(str2.length());   //Result: 3

//This will get the correct number of characters
System.out.println(str2.codePointCount(0, str2.length()));   //Result: 2

codePointCount method

/**
@param begin Start position for length
@param end End position for length
@number of return characters
*/
public int codePointCount(int begin, int end)

Recommended Posts

[Java] Get the length of the surrogate pair string
Get the result of POST in Java
[Java] Get the day of the specific day of the week
How to get the length of an audio file in java
The story of low-level string comparison in Java
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation
[Java] How to get to the front of a specific string using the String class
[Java] How to get the authority of the folder
[Java] How to easily get the longest character string of ArrayList using stream
Get the URL of the HTTP redirect destination in Java
[Java] How to get the maximum value of HashMap
[Java] Get the file in the jar regardless of the environment
Get to the abbreviations from 5 examples of iterating Java lists
[Java] Try editing the elements of the Json string using the library
[Java] Speed comparison of string concatenation
Various methods of Java String class
[Java version] The story of serialization
Various methods of the String class
Get the ID of automatic numbering
[Java] Correct comparison of String type
I read the source of String
The origin of Java lambda expressions
[Java1.8 +] Get the date of the next x day of the week with LocalDate
The story of not knowing the behavior of String by passing Java by reference
Get the public URL of a private Flickr file in Java
I tried to summarize the methods of Java String and StringBuilder
[Java] How to convert from String to Path type and get the path
How to check for the contents of a java fixed-length string
Java string
Memo: [Java] Check the contents of the directory
Compare the elements of an array (Java)
[Java] How to get the current directory
[day: 5] I summarized the basics of Java
[Swift] Get the height of Safe Area
What are the updated features of java 13
Easily measure the size of Java Objects
Looking back on the basics of Java
How to get the date in java
Output of the book "Introduction to Java"
How to get the absolute path of a directory running in Java
[Java] Comparison of String type character strings
I want to get the field name of the [Java] field. (Old tale tone)
The story of writing Java in Emacs
[Java8] Search the directory and get the file
[Java] Check the number of occurrences of characters
[Java] Check if the character string is composed only of blanks (= Blank)
Item 63: Beware the performance of string concatenation
[Java] [Spring] Test the behavior of the logger
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
[Java] Get the date with the LocalDateTime class
Sample code to get the values of major SQL types in Java + MySQL 8.0
[Java] Handling of JavaBeans in the method chain
JAVA: jar, aar, view the contents of the file
The story of making ordinary Othello in Java
[Android] [Java] Manage the state of CheckBox of ListView
About the description order of Java system properties
About the idea of anonymous classes in Java
[Delete the first letter of the character string] Ruby
The order of Java method modifiers is fixed
[Java] Access the signed URL of s3 (signed version 2)