How to obtain the number of bytes of a specified string whose Chinese value is 2

Source: Internet
Author: User
Function: strleft (STR, num)
Purpose: return the number of num characters on the left of the specified string. The Chinese character is used as two characters.
(Such as the last Chinese character to take more, do not take less, then do not take, that is, the actual number of characters to take num-1.
If the first character of the string is Chinese and num = 1, an empty string is returned.

Function strleft (STR, num)

Dim p_str, p_num
P_str = ""
P_num = 0' defines the variable, which indicates the actual number of characters when the Chinese character is 2 bytes.

If trim (STR) <> "then

For I = 1 to num

If ASC (mid (STR, I, 1)> 255 or ASC (mid (STR, I, 1) <0 then' determines the number of bytes for the next character
P_num = p_num + 2
Else
P_num = p_num + 1
End if

If p_num> num then exit

Next

P_str = left (STR, I-1) 'replaces the I-1 with I, resulting in the opposite situation as in the description.

End if

Strleft = p_str

End Function

'------------------ ******* End ******--------------------

Instance:

<%
Function strleft (STR, num)

Dim p_str, p_num
P_str = ""
P_num = 0

If trim (STR) <> "then

For I = 1 to num

If ASC (mid (STR, I, 1)> 255 or ASC (mid (STR, I, 1) <0 then
P_num = p_num + 2
Else
P_num = p_num + 1
End if

If p_num> num then exit

Next

P_str = left (STR, I-1)

End if

Strleft = p_str

End Function

A = "Love late cnblogs Hongfeng"

B = strleft (A, n)
%>
<% = B %>

When N is 3, the result is "love ";
When the N value is 7, the result is "Love late CNB ";
When the N value is 11, the result is "Love late cnblogs ";
When the N value is 14, the result is "Love late cnblogs red ".

========================

'---------------------------------------------------
' Code Filter
'---------------------------------------------------
'Form to HTML
Function htmlencode (fstring)
Fstring = Replace (fstring, CHR (13 ),"")
Fstring = Replace (fstring, CHR (10) & CHR (10), "</P> <p> ")
Fstring = Replace (fstring, CHR (10), "<br> ")
Htmlencode = fstring
End Function
'Html to form
Function tableencode (fstring)
Fstring = Replace (fstring, "", CHR (13 ))
Fstring = Replace (fstring, "</P> <p>", CHR (10) & CHR (10 ))
Fstring = Replace (fstring, "<br>", CHR (10 ))
Tableencode = fstring
End Function
'Html code Filtering
Function filterhtml (strtofilter)
Dim strtemp
Strtemp = strtofilter
While instr (1, strtemp, "<") and instr (1, strtemp, "> ")
Strtemp = left (strtemp, instr (1, strtemp, "<")-1) & right (strtemp, Len (strtemp)-instr (1, strtemp, "> "))
Wend
Filterhtml = strtemp
End Function

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.