可以采用“旁门左道”的方式使用instr函数实现代码的简练。下面是一个典型的例子,检测字符串中是否包含一个元音字母:
1、普通的方法:
if ucase$(char) = "a" or ucase$(char) = "e" or ucase$(char) = "i" or ucase$(char) = "o" or ucase$(char) = "u" then
' it is a vowel
end if
2、更加简练的方法:
if instr("aaeeiioouu", char) then
' it is a vowel
end if
同样,通过单词中没有的字符作为分界符,使用instr来检查变量的内容。下面的例子检查word中是否包含一个季节的名字: 1、普通的方法:
if lcase$(word) = "winter" or lcase$(word) = "spring" or lcase$(word) = _ "summer" or lcase$(word) = "fall" then
' it is a season's name
end if
2、更加简练的方法:
if instr(";winter;spring;summer;fall;", ";" & word & ";") then
' it is a season's name
end if
有时候,甚至可以使用instr来替代select
case代码段,但一定要注意参数中的字符数目。下面的例子中,转换数字0到9的相应英文名称为阿拉伯数字: 1、普通的方法:
select case lcase$(word)
case "zero"
result = 0
case "one"
result = 1
case "two"
result = 2
case "three"
result = 3
case "four"
result = 4
case "five"
result = 5
case "six"
result = 6
case "seven"
result = 7
case "eight"
result = 8
case "nine"
result = 9
end select
2、更加简练的方法:
result = instr(";zero;;one;;;two;;;three;four;;five;;six;;;seven;eight;nine;", ";" & lcase$(word) & ";") \ 6
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器