[java] Summary of how to handle char

When doing competitive programming in java, I felt that the handling of char type was complicated, so I decided to summarize it.

char type can be cast to int type

The char type stores Unicode characters in hexadecimal. At this time, the char type has a numerical value when the Unicode character is converted into a decimal number. For example, if you assign a char type '1' to an int type variable, you can treat it as a numerical value of 49.

int c = 'a';
//49 is output

Convert from corresponding number to char

(char) (numerical value) becomes the char corresponding to the numerical value.

char c = (char)97;
//'a'Is output

Convert char type numbers to int type

There are times when you want to convert a char type number to an int type, such as when you get a number as a char type with String.charAt (index) from a character string that lists the numbers. As mentioned above, if you try to use the char type as an int type as it is, it will be a different number. In this case, Character.getNumericValue (char c) is used.

char c = '1';
int value = Character.getNumericValue(c);
//value == 1

Conversely, if you want to convert an int type number to a char type, add it to '0'.

int a = 1;
char c = (char)('0'+a);
//c == '1'

Handles char type alphabet

The numbers of the char type alphabet are continuous, such as 97 for char type'a'and 98 for'b'. Therefore, each alphabet can be handled by the for statement as follows.

char c = 'a';
for(int i = 0; i < 26; i++){
//abc...Is output

Consider storing in an array how many of each alphabet is contained in a string.

String S = "abbcccdefg";
int[] a = new int[26];
for(int i = 0; i < S.length(); i++)

Even if you don't remember the number of'a', there is no problem because'a' can be treated as a number.

Recommended Posts

[java] Summary of how to handle char
[java] Summary of how to handle character strings
[Java] [Maven3] Summary of how to use Maven3
[Java] Summary of how to abbreviate lambda expressions
Summary of Java communication API (1) How to use Socket
Summary of Java communication API (3) How to use SocketChannel
Summary of Java communication API (2) How to use HttpUrlConnection
Summary of how to implement default arguments in Java
How to make a Java calendar Summary
Summary of how to write annotation arguments
Summary of Java support 2018
Summary of how to create JSF self-made tags
[Java] How to get the authority of the folder
[Java] How to use compareTo method of Date class
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to handle uploaded images
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
[Java11] Stream Summary -Advantages of Stream-
[Java Silver] Summary of points related to lambda expressions
How to minimize Java images
How to write java comments
[Java] Summary of regular expressions
[Java] How to get the maximum value of HashMap
How to use java class
[Java] Summary of operators (operator)
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
Summary of knowledge required to pass Java SE8 Silver
How to use Java Map
As of April 2018 How to get Java 8 on Mac
How to set Java constants
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
How to use Java variables
How to handle an instance
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
[Java] Summary of control syntax
How to initialize Java array
How to execute WebCamCapture sample of NyARToolkit for Java
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
Summary of how to use the proxy set in IE when connecting with Java
An unsupported Java version How to get rid of errors
How to use trained model of tensorflow2.0 with Kotlin / Java
Comparison of how to write Callback function (Java, JavaScript, Ruby)
How to derive the last day of the month in Java
Differences in how to handle strings between Java and Perl
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Studying Java # 6 (How to write blocks)