回复 7楼 吹水佬
CLEAR
Str1 = "人发可个方和方经品他三方登1山泛水多福多寿辅导费对方01234567890"
? CutStr(str1,31)
? CutStr(31,31)
FUNCTION CutStr(cStr1,nLen1,nAdd)
LOCAL N1,cStr2
*!* 强行将参数值转换为字符串,并去掉字符串右边的空格
cStr1 = RTRIM(TRANSFORM(cStr1))
*!* 长度参数错误或者大于字符串长度,取字符串最大长度
N1 = LEN(cStr1)
nLen1 = IIF(TYPE("nLen1") # "N",N1,IIF(nLen1>N1,N1,nLen1))
*!* 截取的最后一个字符是半个双字节时,是舍去还是保留
nAdd = IIF(TYPE("nAdd") # "N",0,IIF(nAdd = 1,1,0))
if N1 > nLen1
LOCAL i,nCount
nCount = 0
*!* 计算字符是双字节还是单字节,是第1位还是第2位
FOR i=1 TO nLen1
IF ISLEADBYTE(SUBSTR(cStr1,i,1)) = .T. &&判断双字节时计数 +1
nCount = nCount + 1
ENDIF
ENDFOR
IF ISLEADBYTE(SUBSTR(cStr1,i,1)) = .T. &&判断是不是双字节
IF MOD(nCount,2)=0 &&判断是否双字节的第2位
cStr2=SUBSTR(cStr1,1,nLen1)
ELSE
cStr2=IIF(nAdd = 0,SUBSTR(cStr1,1,nLen1-1),SUBSTR(cStr1,1,nLen1+1))
ENDIF
ELSE
cStr2=SUBSTR(cStr1,1,nLen1)
ENDIF
ELSE
cStr2 = cStr1
ENDIF
RETURN cStr2
ENDFUNC