About Us | string vbs and JS all provide a function to intercept a length of string, but one problem is that these functions treat Chinese characters as 1 bytes rather than normal 2 bytes, and if the input data is submitted to the database, It is likely that the true length of the data is answered with the maximum length allowed for the field in the database, and there is an error.
I didn't do it at first, and I saw the post of Brother Cheery_ke.
http://www.dev-club.com/club/bbs/showAnnounce.asp?id=1227955
Before we know what to do. The specific procedure is
'--------------------*****start*****--------------------
FUNCTION Strleft (Str,num)
DIM P_str,p_num
P_str = ""
P_num = 0 ' defines a variable and marks the actual number of characters when Chinese 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 of the next character to be taken
P_num = P_num + 2
Else
P_num = p_num + 1
End IF
IF p_num > num THEN EXIT for
NEXT
P_str = Left (str,i-1) ' replaces i-1 with I, which produces the opposite of "description."
End IF
Strleft=p_str
End FUNCTION
'--------------------******end******--------------------
However, in the actual use of cheery_ke found that the procedures provided by brother have some problems: if the number of characters submitted is less than the maximum allowed, the program reports incorrectly because the loop at the for loop is greater than the actual length of the string, resulting in an error with the Mid function's argument I greater than the length of the string in the next sentence.
I have made the following improvements for this program:
function Strleft (str,num)
Dim p_str,p_num
P_str = ""
P_num = 0
If Trim (str) <> "" Then
P_len = Len (str)
For i = 1 to P_len
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
P_str = Left (str,i-1)
Exit For
Else
P_STR = str
End If
Next
End If
Strleft=p_str
End Function
That would not be the problem.
PostScript: I write this article is not in order to raise oneself by pointing out other people's loophole, I am just a learning ASP not how long freshman, and in this forum also got Cheery_ke Brother's help, I am very grateful and admire him. The problem is for some rookie like me to take less detours when encountering this problem, and I have learned this many things in this forum has not been in return, deeply apologetic, so write down their own little experience to do their own mind, as to whether the laughable is unknown.