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

What is the function for processing characters in java

WBOY
WBOYforward
2023-04-18 17:01:03767browse

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();
}
 
}
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete