在模块上使用Mid检查字符串中的数字。 [英] Checking For Numbers in a String Using Mid on a Modual.
本文介绍了在模块上使用Mid检查字符串中的数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是编码的新手,需要弄清楚如何使用mid函数检查文本框中的数字,这需要在模块中,因为系统需要多次执行此功能。
我试过的代码;
公共 < span class =code-keyword> Function NumberCheck(Compleat As Boolean )
Dim StringName As String
Dim 计数,lenofstring 作为 短
Dim 字符作为 字符
Dim 输入 As TextBox
' Mid(StringName,start [,length])
StringName =(< span class =code-string> 输入)
Count = 0
lenofstring = Len(StringName)
对于 a = 1 lenofstring
character = Mid(StringName,a, 1 )
下一步 a
结束 函数
解决方案
您可以使用IsNumeric
方法 [ ^ ]在字符串上,或IsNumber
方法 [ ^每个字符都有。
你的函数应该以String
作为参数(然后你调用它传递TextBox.Text
protperty)。
您可以使用String.IndexOfAny [ ^ ]方法。
例如
公共 函数 chkInt( ByVal s 作为 String )
Dim 找到作为 整数
found = s.IndexOfAny( New Char (){ 0 c, 1 c, 2 c, 3 c, 4 c, 5 c, 6 c,7 c, 8 c, 9 c})
如果找到<> -1 然后 chkInt = True 其他 chkInt = 错误
结束 功能模块中的
:
模块 Module1
Sub Main( ByVal input 作为 字符串)
如果测试(输入)那么
MessageBox.Show( < span class =code-string>它包含数字。)
否则
MessageBox.Show( 它不包含数字。)
结束 如果
结束 Sub
公共 功能测试( ByVal s As String ) As Boolean
返回(s.IndexOfAny( 0123456789 .ToCharArray())< ;> -1)
结束 功能
< span class =code-keyword>结束 模块
$ b表格中的$ b:
私人 Sub Button1_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 Button1.Click
Module1.Main(TextBox1.Text)
结束 子跨度>
I am new to coding and need to work out how to check for numbers in a textbox using a mid function, this will need to be in a module as the system will need to carry out this function many times.
Code I have tried;
Public Function NumberCheck(Compleat As Boolean)
Dim StringName As String
Dim Count, lenofstring As Short
Dim character As Char
Dim Input As TextBox
'Mid(StringName,start[,length])
StringName = ("Input")
Count = 0
lenofstring = Len(StringName)
For a = 1 To lenofstring
character = Mid(StringName, a, 1)
Next a
End Function
解决方案
You could use theIsNumeric
method[^] on the string, or theIsNumber
method[^] on each character.
Your function should take aString
as argument (then you call it passing theTextBox.Text
protperty).
And you may use the String.IndexOfAny[^] method.
E.g.
Public Function chkInt(ByVal s As String) Dim found As Integer found = s.IndexOfAny(New Char() {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c}) If found <> -1 Then chkInt = True Else chkInt = False End Function
in the module:
Module Module1 Sub Main(ByVal input As String) If Test(input) Then MessageBox.Show("It contains number.") Else MessageBox.Show("It does not contain number.") End If End Sub Public Function Test(ByVal s As String) As Boolean Return (s.IndexOfAny("0123456789".ToCharArray()) <> -1) End Function End Module
in the form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Module1.Main(TextBox1.Text) End Sub
这篇关于在模块上使用Mid检查字符串中的数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文