Calculate the actual length of GBK and UTF8 strings implemented by JavaScript.

Source: Internet
Author: User

Calculate the actual length of GBK and UTF8 strings implemented by JavaScript.

As we all know, in Javascript, the string length is not divided into Chinese and English characters, and each character is considered a length, which is not the same as the strlen () function in PHP. The strlen () function in PHP accumulates every 2 of GBK Chinese characters according to the character set, and accumulates every 3 of the Chinese characters of the UTF-8.

Some children's shoes may ask, why do we need to calculate the actual length?

The main purpose is to match the length range of the database. For example, if a field in the GBK database is varchar (10), the length is equivalent to five Chinese characters, and one Chinese character is equal to the length of two letters. For UTF8 databases, the length of each Chinese character is 3.

After learning the above principles, we can calculate the actual length of a string. If the GBK character set encounters a Chinese character plus 2, if the UTF8 Character Set encounters a Chinese character plus 3.
GBK length calculation function:
Copy codeThe Code is as follows:
// Calculate the actual length of the GBK character set
Function getStrLeng (str ){
Var realLength = 0;
Var len = str. length;
Var charCode =-1;
For (var I = 0; I <len; I ++ ){
CharCode = str. charCodeAt (I );
If (charCode> = 0 & charCode <= 128 ){
RealLength + = 1;
} Else {
// If it is Chinese, the length is increased by 2
RealLength + = 2;
}
}
Return realLength;
}

UTF8 length calculation function:
Copy codeThe Code is as follows:
// UTF8 character set actual length calculation
Function getStrLeng (str ){
Var realLength = 0;
Var len = str. length;
Var charCode =-1;
For (var I = 0; I <len; I ++ ){
CharCode = str. charCodeAt (I );
If (charCode> = 0 & charCode <= 128 ){
RealLength + = 1;
} Else {
// If it is Chinese, the length is increased by 3
RealLength + = 3;
}
}
Return realLength;
}


Use javascript to convert webpage encoding from GBK to UTF-8, append to 200 points

Byte [] B;
String utf8_value;
Utf8_value = request. getParameter ("NAME"); // obtain the UTF8 data of "NAME" from the HTTP stream
B = utf8_value.getBytes ("8859_1"); // use the ISO-8859-1 in the middle of the transition
String name = new String (B, "GB2312"); // convert to GB2312

This is a section of a project program I am working on:
Byte [] B;
String gbk_value;
Gbk_value = request. getParameter ("address"); // obtain the GBK data of "name" from the HTTP stream (due to the web. filters in xml set the default encoding to GBK, so the Internet from UTF-8 to GBK)
B = gbk_value.getBytes ("GBK"); // use GBK in the middle to convert from GBK to GBK Array
String address = new String (B, "UTF-8"); // convert to UTF-8
Myform. setAddress (address );

When the stream length is known, the input stream is converted into a byte array. The input stream abstract class InputStream in Java has the int read (byte [] B, int off, int len) method, in the parameter, byte [] B is used to store data read from InputStream. int off specifies the offset address of array B, that is, the starting subscript of array B, int len specifies the length to be read. The method returns the actual number of bytes read.

A friend who just learned Java may want to say: first define a byte array with the stream length, call the read method, specify the starting subscript as 0, and specify the read length and array length, can it be read at once? I tried to read the data in this way, but it is not safe to read network data later. We think it may not be so smooth to obtain data on the network, data streams may be transmitted intermittently, so it cannot be guaranteed that all data can be read at a time, especially when reading large data volumes. Therefore, we must check the actual read length when reading data, if you have not read the data of a known length, you should read the data again. This loop is detected until the actual read length is accumulated to be equal to the known length. The following code implements this function:

ServletInputStream inStream = request. getInputStream (); // retrieves the HTTP request stream
Int size = request. getContentLength (); // get the HTTP request stream Length
Byte [] buffer = new byte [size]; // used to cache data read each time
Byte [] in_ B = new byte [size]; // array used to store results
Int count = 0;
Int rbyte = 0;
While (count <size ){
// Read cyclically
Rbyte = inStream. read (buffer); // The actual read length is stored in rbyte.
For (int I = 0; I
If you do not know the stream length, enter... the remaining full text>

Write a string verification function in javascript. You must enter 6 to 20 characters in length and contain at least three letters.

Function fnValidate (str ){
Var pattern =/^ (? =. {6, 20} $) ([^ a-z \ r \ n] * [a-z] [^ a-z \ r \ n] *) {3 ,} /I;
// Alert (pattern. test (str ));
Return pattern. test (str );
}

Ask questions if you have any questions.

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: 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.