Java implementation string inversion

Source: Internet
Author: User

1. Java Encoding method

reference 1:

Java uses the Unicode character set, which uses UTF-16 encoding in memory: one character occupies 2 bytes, and one common Chinese character occupies 2 bytes, but the characters in the very large coded character set are 4 bytes. That UTF-16 encoded Form


Number of characters

Number of bytes

Bit bits





Common Chinese characters




Large character set (uncommon characters, etc.)




Byte is a byte, and the string needs to be converted to a byte array before the network is transmitted or stored. A byte array needs to be converted to a string after it is received from the network or read from the storage device. String is a string that can be viewed as an array of char. String and char are memory forms, and byte is the serialized form of network transport or storage. string serialization into a byte array or deserialization needs to choose the correct encoding method。 If the encoding is not correct, you will get some 0x3f values. The commonly used character encoding methods are Iso8859_1, GB2312, GBK, utf-8/utf-16/utf-32.
2, String reversal reference: Http:// Inversion method StringBuilder in the parent class, the source code is as follows:
public Abstractstringbuilder reverse () {Boolean hassurrogate = false;    int n = count-1;        for (int j = (n-1) >> 1; J >= 0;--j) {Char temp = value[j];        Char temp2 = value[n-j]; if (!hassurrogate) {hassurrogate = (temp >= character.min_surrogate && temp <= Character.max_surrogat E) | |         (Temp2 >= character.min_surrogate && temp2 <= character.max_surrogate);         } Value[j] = Temp2;     VALUE[N-J] = temp; } if (hassurrogate) {//Reverse back all valid surrogate pairs for (int i = 0; i < count-1; i++             ) {char C2 = value[i];                 if (character.islowsurrogate (C2)) {Char C1 = value[i + 1];                 if (Character.ishighsurrogate (C1)) {value[i++] = C1;             Value[i] = C2; }}}} return this; }
*************************************************************************************************************** ****My Implementation:
/**  * Created: August 15, 2014 PM 9:44:51  * Project name: Test  * @author Cao yanfeng  * @since JDK 1.6.0_21  * Class Description:  *  /public class Accuracytest {     /**     * @param args */public    static void Main (string[] args) {        //TODO auto-generated method Stub        String string= "Caoyanfeng Cao Yanfong Peking University
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.