使用LISP进行字符串处理时,遇到中文时很麻烦,系统自带函数SubStr、StrLen都无法处理带中文的字符串。为此做了两个函数,分享给大家。
- (defun SubStrEx(Input Start Len / Result REG DATA MaxIndex);
- ;vbscript.regexp 对象由Microsoft VBScript Regular Expressions5.5提供
- ;保存于c:\windows\system32\vbscript.dll
- ;\S|\s 可以匹配任意字符
- ;Start开始字符串,从1开始(提前要做好类型判断)
- ;Len表示获取到的总长度(提前要做好类型判断)
- (setq Result "")
- (setq Start (1- Start))
- (if (< Start 0)
- (setq Start 0)
- )
- (setq REG (vlax-create-object "vbscript.regexp"))
- (vlax-put-property REG 'Pattern "\\S|\\s")
- (vlax-put-property REG 'Global :vlax-true);是否全部匹配
- (setq DATA (vlax-invoke-method REG 'Execute Input))
- (setq MaxIndex (vlax-get-property DATA 'Count))
- (while (and (> Len 0) (< Start MaxIndex))
- (setq Result (strcat Result (vlax-get-property (vlax-get-property DATA 'Item Start) 'Value)))
- (setq Start (1+ Start))
- (setq Len (1- Len))
- )
- (vlax-release-object DATA)
- (vlax-release-object REG)
- Result
- )
- (defun StrLenEx(Input / Result REG DATA)
- ;获取字符串的长度
- (setq Result nil)
- (setq REG (vlax-create-object "vbscript.regexp"))
- (vlax-put-property REG 'Pattern "\\S|\\s")
- (vlax-put-property REG 'Global :vlax-true);是否全部匹配
- (setq DATA (vlax-invoke-method REG 'Execute Input))
- (setq Result (vlax-get-property DATA 'Count))
- (vlax-release-object DATA)
- (vlax-release-object REG)
- Result
- )
|