VB截取中英文混合字符串中若干半角单位长字串的函数

前端之家收集整理的这篇文章主要介绍了VB截取中英文混合字符串中若干半角单位长字串的函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://blog.csdn.net/jhycjhyc/article/details/3789937

Public Function leftMix(ByVal sourceString As String,ByVal mixlen As Integer) As String

'此函数用于从中英文混合字串中从左开始以半角字符的长度为单位截取若干字符,以避免该字符写入sql表中定长文本字段时可能出现的错误.
'leftmix("唐纽市布通街132号广商局宿舍2幢2单元3楼1号",39)="唐纽市布通街132号广商局宿舍2幢2单元3楼1"
'一个常见的例子 .fields("地址")=leftmix((xlSheet.Cells(i,7)),50)
'须注意的是,须在模块中有此声明:Public Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
Dim i As Integer
sourceString = Trim(sourceString)
For i = 1 To lstrlen(sourceString)
If lstrlen(sourceString) > mixlen Then
sourceString = Left(sourceString,Len(sourceString) - 1)
Else
Exit For
End If
Next i
leftMix = sourceString
End Function

猜你在找的VB相关文章