分享:截取字N长度的字符串
CLEAR
Str1 = "12345人发可个方和方经品他三方登山泛水多福多寿辅导费对方012345678901nLen1-1456789"
? CutStr(str1,31)
? CutStr(31,31)
*!* 截取 N 长度的字符串,中英文通杀
FUNCTION CutStr(cStr1,nLen1)
LOCAL N1,cStr2
*!* 处理参数
IF TYPE("cStr1") # "C"
IF EMPTY(cStr1)
cStr1 = ""
ELSE
cStr1 = ALLTRIM(TRANSFORM(cStr1))
ENDIF
ENDIF
N1 = LEN(cStr1)
IF TYPE("nLen1") # "N"
nLen1 = N1
ELSE
IF nLen1 > N1
nLen1 = N1
ENDIF
ENDIF
if N1>nLen1
LOCAL i,Nm,ary[nLen1]
Nm=1
FOR i=1 TO nLen1
IF ASC(SUBSTR(cStr1,i,1))>126
ary[i]=Nm
Nm=Nm+1
ELSE
ary[i]=0
ENDIF
ENDFOR
IF ASC(substr(cStr1,nLen1,1)) > 126
IF ary(nLen1)>0 and MOD(ary(nLen1),2)=0
cStr2=SUBSTR(cStr1,1,nLen1)
ELSE
cStr2=SUBSTR(cStr1,1,nLen1-1)
ENDIF
ELSE
cStr2=SUBSTR(cStr1,1,nLen1)
ENDIF
ELSE
cStr2 = cStr1
ENDIF
RETURN cStr2
ENDFUNC