Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1603487
  • 博文数量: 184
  • 博客积分: 3044
  • 博客等级: 中校
  • 技术积分: 2467
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-25 15:04
文章分类

全部博文(184)

文章存档

2022年(4)

2021年(3)

2020年(1)

2019年(5)

2018年(13)

2017年(6)

2016年(10)

2015年(11)

2014年(11)

2013年(13)

2012年(23)

2011年(25)

2010年(2)

2008年(1)

2007年(5)

2006年(51)

分类:

2006-04-08 01:40:05

VBscript中的字符串函数

在VBscript中,系统提供了大量的字符串函数来处理有关字符串的事情。在 Javascript 中,系统为String对象提供了许多方法,而字符串变量可以不用附加说明就使用这些方法,使对字符串的处理能力更加强大。下面我们分批介绍这些函数和方法。

目标任务1  演示子字符串的截取,字符串的截空,子串的定位等。

关键字 left, right, mid, space, trim, instr, len

代码

dim Mystr, Myword,Mypos, BR
Mystr = "The built-in objects are special because they are built into ASP pages and do not need to be created before you can use them in scripts."
BR = "

"
document.write( "Mystr=" &chr(34) & Mystr & chr(34) &BR)
document.write("字符串Mystr的长度是" & len(Mystr) & BR)
document.write("7位左子串是" & left(Mystr,7) &BR)
document.write("8位左子串是" & right(Mystr,8) &BR)
document.write("从第5位开始的12个字符是" & mid(Mystr, 5, 12) &BR)
Myword = space(3) & "hello" & space(2)
document.write(chr(34) & Myword &chr(34))
document.write("这个字符串的的长度是" & len(Myword) &BR)
document.write("截去前导空格后为:" & chr(34) &Ltrim(Myword)&chr(34) &BR)
Myword = "OBJECT"
document.write ("The Myword =" & chr(34)&Myword&chr(34) )
Mypos = Instr(Mystr,Myword)
if Mypos=0 then
document.write(" Myword不是它的子串")
else
document.write(Myword & "是子串,第一个开始于" & Mypos)
end if
document.write BR
Myword = LCase(Myword)
Mypos = Instr(Mystr,Myword)
if Mypos=0 then
document.write(" Myword不是它的子串"&BR)
else
document.write(Myword & "是子串,第一个开始于" & Mypos)
end if

观看代码的运行结果

代码注释

这段代码演示了VBscript中的许多字符串函数的用法,用左子串left,右子串right,任意子串mid,生成空格字符串space,截去前导空格Ltrim,截去尾部空格Rtirm,截去前后空格tirn,字符串长度len,判断子字符串的存在性和出现的位置Instr。大小写转换Ucase 和Lcase,把ASCII码转换为字符的函数chr。

mid函数从字符串中返回指定数目的字符,语法:Mid(string, start[, length])
InStr函数返回子串在主字符串中第一次出现的位置,如子串不存在则返回0。语法:
    InStr([start, ]string1, string2[, compare])
省略开始位置则从开始,compare=0 or 1,表示有二进制比较(0)或进行文本比较(1),缺省为二进制比较。详见语法参考。

其它函数的语法从例程中一目了然,不再赘述。


目标任务2  演示用spilt函数分割字符串

关键字  split

代码

Private Sub CommandButton1_Click()
Dim Mystr, Myword, Mypos, BR
Mystr = "192.168.61.18:/usr/eas51:/opt/IBM"
MyString = Split(Mystr, ":", -1, 1)
For i = 0 To UBound(MyString)
TextBox1.Text = TextBox1.Text + MyString(i) & vbCrLf
Next

End Sub

代码注释

  • split函数的功能是按照给定的分割符切割字符串,结果返回给一个数组。语法:
        数组 = split(字符串,分割符,count,compary)
    count是要求返回的子串个数,-1表示全部;compary=0|1,表示执行二进制|文本比较。
  • uBound(数组)返回数组的上界

观看 Sdemo1


目标任务3  演示字符串逆转函数strReverse

关键字 strReverse

代码

sub sdemo2   
  dim BT,yourin,yourout
  BT = "
"
  yourin = InputBox("请任意输入一个字符串,中文也可以")
  yourout = strReverse(yourin)
  MsgBox(yourout)
End sub

代码注释

strReverse函数返回按相反顺序排列的字符串,对中文也可以用。

观看 Sdemo2


目标任务4  演示字符串的替换和比较

关键字 replace, strcomp, string

代码

sub sdemo3    
dim BT,olrstr, newstr
BT = "
"
oldstr= "Hello"
newstr=string(3,"-")&oldstr&string(3,"-")
MsgBox(newstr)
newstr = replace(newstr, oldstr, Ucase(oldstr)&" World")
MsgBox(newstr)
newstr = Ucase(oldstr)
for i=0 to 1
result =strcomp(newstr, oldstr,i)
if result=0 then
MsgBox(newstr &"与"&oldstr& "相等 with "&i)
else
MsgBox(newstr &"与"&oldstr& "不相等 with "&i)
End if
Next
End sub

代码注释

  • String(个数,单个字符)返回有相同字符组成的字符串。接着把Hello替换成大写并显示出来。
  • replace函数的语法:
        Replace(expression, find, replacewith)
    expression是待搜索文本, find是要查找的子串, replacewith是用来替换的子串。更详细的语法请查阅相关文章。
  • 最后一段演示字符串比较函数
        Strcomp(str1,str2,i)
    str1和str2是两个被比较的字符串,i=0或1表示执行二进制(0)或文本(1)进行比较。i=1时不区分大小写,在程序的消息框中报告了这一点,请读者在运行下面的sdemo3时注意阅读。
阅读(2228) | 评论(0) | 转发(1) |
0

上一篇:sqlplus

下一篇:批处理

给主人留下些什么吧!~~