举例说明常用字符串处理函数
2011-01-14 18:15 javaspring 阅读(272) 评论(0) 编辑 收藏 举报Option Explicit
Private Sub Command1_Click()
Dim strtest As String, strtest1 As String, strtest2 As String
Dim strtest3 As String, strtest4 As String, strtest5 As String
Dim strtest6 As String, strtest7 As String, strtest8 As String
strtest = "abc123abc123"
List1.AddItem "Left(strtest,5)=" & Left(strtest, 5)
List1.AddItem "Right(strtest,5)=" & Right(strtest, 5)
List1.AddItem "Mid(strtest,5,2)=" & Mid(strtest, 5, 2)
List1.AddItem ""
strtest1 = " abc 123 ": strtest2 = "abc"
List1.AddItem "Ltrim(strtest1)=" & LTrim(strtest1) & strtest2 '去掉字符串前空格
List1.AddItem "Rtrim(strtest1)=" & RTrim(strtest1) & strtest2 '去掉字符串后空格
List1.AddItem "Trim(strtest1)=" & Trim(strtest1) & strtest2 '同时去掉字符串前后空格
List1.AddItem ""
strtest3 = "abc 123": strtest4 = "ABC 123"
List1.AddItem "Len(strtest3)=" & Len(strtest3) '返回字符串的字符个数
List1.AddItem "LenB(strtest3)=" & LenB(strtest3) '返回字符串所占字节数
List1.AddItem "Ucase(strtest3)=" & UCase(strtest3) '返回字母大写
List1.AddItem "Lcase(strtest4)=" & LCase(strtest4) '返回字母小写
List1.AddItem ""
'注意Str和Cstr的区别
strtest5 = "123abc 123"
List1.AddItem "InStr(strtest5,strtest3)=" & InStr(strtest5, strtest3) '在第一个字符串中查找第二个字符串,返回找到的字符位置
List1.AddItem "InStr(7,strtest5,strtest3)=" & InStr(7, strtest5, strtest3) '7表示从第7个字符开始找
List1.AddItem "InStr(2,strtest5,strtest3)=" & InStr(2, strtest5, strtest3) '2表示从第2个字符开始找
List1.AddItem "InStrB(strtest5,strtest3)=" & InStrB(strtest5, strtest3) '返回找到的字节位置
List1.AddItem ""
Dim strarry(2) As String
strarry(0) = "123": strarry(1) = "abc": strarry(2) = "ABC"
strtest6 = "abc-123-ABC"
List1.AddItem "join(strarry(),-)=" & Join(strarry(), "-") '合并字符串,-分隔符,缺省为空格
List1.AddItem ""
List1.AddItem "format(100000)=" & Format(10000) '省略格式控制符字符串,默认返回字符串
List1.AddItem "format(100000" & "," & """" & "General Number" & """" & ")=" & Format(10000, "General Number")
List1.AddItem "format(100000" & "," & """" & "Currency" & """" & ")=" & Format(100000, "Currency")
'其他详见msdn,或者大一vb课本P48
List1.AddItem ""
strtest7 = "ABC": strtest8 = "123dfd43"
List1.AddItem "Asc(strtest7)=" & Asc(strtest7) '返回字符串的第一个字符的asc2的值
List1.AddItem "Chr(65)=" & Chr(65) '返回asc2值对应的字符
List1.AddItem "Val(strtest8)=" & Val(strtest8) '返回字符串第一个字符前的所有数字
List1.AddItem ""
'StrConv字符串之间相互转换,用于不同语言版本的转换,也就是改变编码方式
Dim in_strconv() As Byte
in_strconv() = "ABC中国"
List1.AddItem "原字符串所占字节=" & CStr(UBound(in_strconv()) - LBound(in_strconv()) + 1)
Dim out_strconv() As Byte
out_strconv() = StrConv(in_strconv(), vbFromUnicode)
List1.AddItem "转换后字符串所占字节=" & CStr(UBound(out_strconv()) - LBound(out_strconv()) + 1)
End Sub