[JAVA] I want to find out if the specified character string is supported by the target character code

If you want to find out if the specified string is supported by the target character code, it is convenient to use CharsetEncoder.canEncode. This is a method that returns true if the method given as an argument is supported by the target character code, false otherwise, for example, if you want to check whether the string s is supported in Shift_JIS, the following You can write as follows.

String s = "...";
Charset.forName("Shift_JIS").newEncoder().canEncode(s);

As an example of verifying that it is actually working, we will use the kanji "Taka". It's a so-called "ladder". This Kanji is not supported in Shift_JIS, but it is supported in its extension Windows-31J. Let's check this.

Charset.forName("Shift_JIS").newEncoder().canEncode("Taka");   //=> false
Charset.forName("Windows-31J").newEncoder().canEncode("Taka"); //=> true

It seems to be working as expected (´ ・ ω ・ `)

** Environmental Information: **

C:\>javac -version
javac 11.0.3
C:\>java -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.3+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.3+7, mixed mode)

Recommended Posts

I want to find out if the specified character string is supported by the target character code
I want to find out what character the character string appears from the left
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to find out which version of java the jar file I have is available
When the character string passed to C ++ by JNA is garbled
[Ruby] I want to output only the odd-numbered characters in the character string
[Java] How to cut out a character string character by character
I want to output the character number from the left where an arbitrary character string appears.
[JavaScript] I want to limit the processing by checking the input items when the specified time elapses at the time of focus out.
(´-`) .. oO (I want to easily find the standard output "Hello".
Find out all timezone IDs supported by the Java TimeZone class
I want to limit the input by narrowing the range of numbers
I want to convert InputStream to String
[Ruby] I tried to diet the if statement code with the ternary operator
[Java] Check if the character string is composed only of blanks (= Blank)
[Java] Divide a character string by a specified character
[Swift] Copy the character string to the clipboard
I want to get a list of only unique character strings by excluding fixed character strings from the file name
[Ruby] I want to make an array from a character string with the split method. And vice versa.
[RxSwift] I want to deepen my understanding by following the definition of Observable
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to sort by tab delimited by ruby
I want to output the day of the week
I want to var_dump the contents of the intent
I want to simply write a repeating string
The code I used to connect Rails 3 to PostgreSQL 10
I want to truncate after the decimal point
I want to delete files managed by Git
I want to get the value in Ruby
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I can't input Japanese with VS code (Visual Studio Code) of Ubuntu 18.04.5! ?? If you want to download VS Code to Ubuntu, go to the official website! !!