The function of this function is to intercept the specified English-Chinese mixed string and keep the display length to one. That is to intercept one Chinese character as two English words. Description: The function of this function is to intercept the specified English-Chinese mixed string and keep the display length to one. That is to intercept one Chinese character as two English words.
Purpose: Generally used in title display lists to avoid uneven insertion of intercepted strings.
program code
Copy the code code as follows:
'//A_strString string to be processed
'//A_intLen is based on the number of English characters
'//A_strAddString If A_strString has a suffix added when intercepting characters, such as:..., it can be empty
function CutString(byval A_strString,byval A_intLen,byval A_strAddString)
dim MM_objRe,MM_objMs,MM_objMh
dimMM_strCut,MM_intLen
setMM_objRe=new RegExp
MM_objRe.Global=true'global search
MM_objRe.IgnoreCase=true' is not case sensitive
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_objMs=MM_objRe.execute(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 for
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
setMM_objRe=nothing
end function
This is different from what is currently popular on the Internet. Regular expressions are used to obtain non-ANSI characters (the default non-ANSI characters here are Chinese characters. If there are deviations, the regular expressions can be modified to achieve the purpose). First of all, please note: the specified length of the function is based on the number of English characters. That is, it is designated as 10, which means 10 English or 5 Chinese characters.
Two optimizations have been made:
The first point: Get the characters of the specified length directly, because whether it is all Chinese characters or all English, the longest is only the length of all English, and the characters exceeding it are absolutely eliminated characters.
Second point: Use regular expressions to find Chinese characters within the range, and only traverse the number of words of each Chinese character, which can minimize the number of loops.