Note: This function is used to intercept a specified English-Chinese mixed string and keep the display length one to one. It is to extract a Chinese character as a two-byte string.
Purpose: this function is generally used to display the list of titles, which can avoid the insertion of string parameters.
Program Code
Copy code The Code is as follows: '// a_strstring the string to be processed
'// A_intlen is based on the number of English characters
'// A_straddstring if a_strstring has a suffix added when it is intercepted, such as:..., it can be blank
Function cutstring (byval a_strstring, byval a_intlen, byval a_straddstring)
Dim mm_objre, mm_objms, mm_objmh
Dim mm_strcut, mm_intlen
Set mm_objre = new Regexp
Mm_objre.global = true 'Global search
Mm_objre.ignorecase = true' case insensitive
Mm_objre.pattern = "[^ \ x00-\ xFF]"
Mm_intlen = a_intlen
If Len (a_strstring) <= a_intlen then
Mm_strcut = a_strstring
Else
Mm_strcut = left (a_strstring, mm_intlen)
Set mm_objms1_mm_objre.exe cute (mm_strcut)
If mm_objms.count <> mm_intlen then
For each mm_objmh in mm_objms
If mm_objmh.firstindex <mm_intlen then
Mm_intlen = MM_intLen-1
Else
Exit
End if
Next
Else
Mm_intlen = mm_intlen/2
End if
Mm_strcut = left (a_strstring, mm_intlen) & a_straddstring
End if
Cutstring = mm_strcut
Set mm_objre = nothing
End Function
This is not the same as what is currently popular on the Internet. The regular expression is used to obtain non-ANSI characters (here the default non-ANSI characters are Chinese characters. If there is any deviation, you can modify the regular expression to achieve the goal ). NOTE 1: The length specified by the function is based on the number of English characters. That is, 10 characters are specified and the meaning is 10 English or 5 Chinese characters.
Two optimizations were made:
The first point is to directly obtain characters of the specified length, because no matter whether it is Chinese or full English, the maximum length is only the full English length. The characters exceeding the length are definitely obsolete characters.
Second, use regular expressions to search for Chinese characters in the range and traverse only the words of each Chinese character. This can minimize the number of cycles.