Home > Java > javaTutorial > What is the function for processing characters in java

What is the function for processing characters in java

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-18 17:01:03
forward
861 people have browsed it

1. getBytes is a standard function for Java string processing. Its function is to encode the characters represented by the string according to charset and express them in byte form.

Note: Strings are always stored in java memory in unicode encoding.

2. newString combines and identifies the byte array according to the charset encoding and converts it to unicode storage.

3. setCharacterEncoding()

This function is used to set http request or corresponding encoding.

Example

package com.test.bs;
 
import java.io.UnsupportedEncodingException;
 
public class UnicodeTest2 {
 
public static void main(String[] args) {
String a = "哈哈";
try {
byte[] gb2312 = a.getBytes("GB2312");
byte[] utf = a.getBytes("UTF-8");
for (int i = 0; i < gb2312.length; i++) {
System.out.print(gb2312[i]);
}
System.out.println();
 
for (int i = 0; i < utf.length; i++) {
System.out.print(utf[i]);
}
System.out.println();
 
System.out.println(new String(gb2312));
System.out.println(new String(utf));
System.out.println(System.getProperty("file.encoding"));//当前文件的编码方式
System.out.println(new String(utf, "UTF-8"));
System.out.println(new String(gb2312, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
 
}
}
Copy after login

The above is the detailed content of What is the function for processing characters in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template