Str. getbytes (). Length! in different environments! = Str. Length () full-width half-width judgment

Source: Internet
Author: User
Str. getbytes (). Length! in different environments! = Str. Length?
In the test environment, it is okay to determine whether there is a full angle. The results are different in the formal environment.
Both are UNIX systems, but the system languages are different.
Is there a way to make the running results consistent in different environments?
That is to say, the encoding to be used is the same.
The page is coded in UTF-8.
Str. getbytes ('utf-8'). length! = Str. Length,
You still need Str. getbytes ('unicode '). length! = Str. Length ().
Getbyte () uses the default encoding.
Use UTF-8 most insurance:
Str. getbytes ("UTF-8"). length! = Str. Length ();
There is a problem with Unicode. The following inequality will always be true regardless of the character.
Str. getbytes ("Unicode"). length! = Str. Length ();

In addition, str. getbytes (). length has different lengths in different environments.
Str. getbytes (). Length
In Windows XP, a full-width character is 2 characters in length;
In the liunx environment, a full-width character is 3 characters in length.

Because it is in the Japanese operating system, the Code ms932 is added.
Str. getbytes ("ms932"). Length
In this way, the length of the two system environments is 2.


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.