Two ways to get string lengths by Byte in Java

Source: Internet
Author: User

The JDK itself comes with an API that gets the length of the string byte, but if the string contains special symbols or full-width symbols or punctuation, the result is biased, and the best evidence is Sina Weibo's word count.
JDK fetch byte length //Note getBytes () is obtained by default in the file encoding format of bytes, usually UTF-8 (can look at the API), different encoding format of bytes, the length obtained is also different, I have personally tested it, it is recommended to obtain a bytes length with the specified encoding format, such as: GetBytes ("UTF-8")int length = new String (). GetBytes (). length; If the content contains newline characters, tabs, carriage returns and other symbols, to remove, you can use the following code://Clean up the content of useless newline characters and so on to get more accurate length
content = Content.replaceall ("[\r\n\t]", ""); In order to solve this problem, perfect found a solution

Two ways to get string lengths by Byte in Java

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: info-contact@alibabacloud.com 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.