Calculate the length of a string containing Japanese characters using VB. NET.

Source: Internet
Author: User

Calculate the length of a string containing Japanese characters using VB. NET.

I can't find the previous code, but I can only write it myself. (finding someone else's code is a little troublesome and useless)

String. Prototype. isbytes = function (){
VaR Carr = This. Match (/[^ \ x00-\ xFF | \ uff61-\ uff9f]/ig );
Return (CARR = NULL? True: false );}
The above code was found in the Java project and I didn't understand it at first.
My friend Lao Ji sent me a message.
Public static Boolean checkascii (char ch ){
// The ASCII text cannot be used to determine whether the Delimiter is correct or not.
Return ch> = 0x0000 & Ch <= 0x007f;
}

Public static Boolean checkhankaku_kana (char ch ){
// The half-width corner of the page indicates whether the cursor is correct or not.
Return 0xff61 <= CH & Ch <= 0xff9f;
}
Then we will know that the regular expression [^ \ x00-\ xFF | \ uff61-\ uff9f] matches characters other than English and half-width Japanese.
Note the inverse meaning of the symbol ^ [\ x00-\ xFF | \ uff61-\ uff9f]. No ^ matches English characters and half-width Japanese characters.
Add this ^ to match characters other than English characters and half-width Japanese characters.
Below is the length of the string calculated using the VB. NET method I wrote.
Public Function getstringlength (byval data as string) as integer
Dim I as integer = 0
Dim Len as integer = 0
Dim cc as string = ""
Dim charregex as new RegEx ("[\ x00-\ xFF | \ uff61-\ uff9f]")
For I = 0 to data. Length-1
The text token is used to obtain the delimiter row.
Cc = data. substring (I, 1)
If charregex. ismatch (CC) then
Len = Len + 1
Else
Len = Len + 2
End if
Next I
Return Len
End Function
In this way, the length of the string can be calculated (if any is incorrect, point it out)

Later, my friend sent me another code segment, as shown below:
Public Function getbyte (byval P_S) as integer
Dim bysource () as byte
Dim byencoded () as byte
Dim destencoding as encoding more. net source code and instances, from Lebo www.lob.cn

Text column comment Comment comment
Bysource = encoding. Unicode. getbytes (P_S)

エンコ (シフ JIS ドペー)
Destencoding = destencoding. getencoding ("shift_jis ")

Please refer to the following link for more information: Unicode Character character JIS character
Byencoded = encoding. Convert (encoding. Unicode, destencoding, bysource)

Return byencoded. Length

End Function

Public Function chkbytelength (byval p_strval as string, byval p_strparam as string, byval p_nmaxlength as integer, optional byval p_nminlength as integer = 0) as Boolean
If p_nminlength> 0 then
If getbyte (p_strval)> p_nmaxlength or getbyte (p_strval) <p_nminlength then
M_amsg.add (Getline () & clsmessage. getmessage ("e018", p_strparam, CSTR (p_nminlength), CSTR (p_nmaxlength )))
Return false
End if
Else
If getbyte (p_strval)> p_nmaxlength then
M_amsg.add (Getline () & clsmessage. getmessage ("e009", p_strparam, CSTR (p_nmaxlength )))
Return false
End if

End if
Return true
End Function
These two methods have not been tried. If you are interested, try them.

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.