VB中inStr和nStr有什么区别?内详。知道的帮忙下,谢谢。

来源:百度知道 编辑:UC知道 时间:2024/06/03 13:06:25
VB中inStr和nStr有什么区别?

nStr1 As String
nStr1 = 0
Input1:
nStr1 = InputBox("First number", , nStr1)
If nStr1 = "" Then Exit Sub
If InStr(nStr1, ".") > 0 Then MsgBox "Please input an integer": GoTo Input1
Num1 = Val(nStr1)

instr 是一个VB内置函数,负责返回 指定一字符串在另一字符串中最先出现的位置。

使用方法 InStr([start, ]string1, string2[, compare])

如:
Private Sub Form_Load()
MsgBox InStr("PromiseForever", "i")
End Sub
将会显示5.

你说的nStr呢,是上面这段代码的一个普通变量,你可以叫其他的也没问题
这段代码中,这个主要是获取输入的数值,可以说这段代码写的相当不规范。
给你注释了,有不懂的地方留言
Private Sub Form_Load()
'声明一个变量为nStr1
Dim nStr1 As String
'初始化变量
nStr1 = "0"
'Input1:过程
Input1:
'获取用户输入内容
nStr1 = InputBox("First number", , nStr1)
'如果输入为空,退出过程
If nStr1 = "" Then Exit Sub
'如果输入数字包含小数点,提示输入一个整数并重新开始输入
If InStr(nStr1, ".") > 0 Then MsgBox "Please input an integer": GoTo Input1
'转换变量并保存到另外一个Num1变量中
Num1 = Val(nStr1)
'注释 by firendless
End Sub

如果想要找稍微标准一点的inputbox使用可以到我的旧博客里找inputBoxEx函数