使用类CheckInput.vb
Public Class CheckInputClass CheckInput
读取数字串#Region "读取数字串"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnNumber()Function ReturnNumber(ByVal str As String) As Double 'Integer 太小,可容纳的数字量少
'返回值:
' -1:表示数字0
' -2:表示字符为空
' -3:表示有一个字符,且不是数字
Dim i As Integer = str.Length
Dim j As Integer
Dim numstr As String = str.Substring(0, 0)
Dim checkstr As String
If i = 0 Then
Return -2 ' -2:表示字符为空
End If
If str <> "0" Then
If i = 1 Then
If Char.IsNumber(str) = False Then
Return -3 ' -3:表示有一个字符,且不是数字
Else
Return Convert.ToInt32(str)
End If
ElseIf i >= 2 Then
For j = 0 To i - 1
checkstr = str.Substring(j, 1)
If Char.IsNumber(checkstr) = True Then
numstr += checkstr
End If
Next
If numstr.Length <> 0 Then
Return Convert.ToDouble(numstr)
Else
Return -1
End If
End If
Else '表示字符串为 "0"
Return -1 ' -1:表示数字0
End If
End Function
#End Region
读取字符串#Region "读取字符串"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnStr()Function ReturnStr(ByVal str As String) As String
'返回数字0表示字符串为空
'否则返回所有字符串.其他字符过滤
If str.Length = 0 Then
Return "0"
End If
Dim rtnStr As String = ""
Dim chkStr As String
Dim i As Integer = str.Length
Dim j As Integer
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
'不是数字,空格,符号
If Char.IsNumber(chkStr) = False And Char.IsWhiteSpace(chkStr) = False And Char.IsPunctuation(chkStr) = False And Char.IsSymbol(chkStr) = False Then
rtnStr += chkStr
End If
Next
If rtnStr.Length <> 0 Then
Return rtnStr
Else
Return "0"
End If
End Function
#End Region
读取符号#Region "读取符号"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnSymbol()Function ReturnSymbol(ByVal str As String) As String
If str.Length = 0 Then
Return "0" '字符串空
End If
Dim rtnStr As String = ""
Dim chkStr As String
Dim i As Integer = str.Length
Dim j As Integer
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsNumber(chkStr) = False And Char.IsWhiteSpace(chkStr) = False And Char.IsLetter(chkStr) = False Then
rtnStr += chkStr
End If
Next
If rtnStr.Length <> 0 Then
Return rtnStr
Else
Return "0"
End If
End Function
#End Region
按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串#Region "按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串"
/**/''' <summary>
''' 按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串
''' </summary>
''' <param name="str"></param>
''' <param name="type">类型:Lower(小写字母),Upper(大写字母),All(所有字母)</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnChar()Function ReturnChar(ByVal str As String, ByVal type As String) As String
Dim chkStr As String
Dim rtnStr As String = ""
Dim i As Integer = str.Length
Dim j As Integer
If i = 0 Then
Return "String Input Error,Please check again."
End If
If type.ToString = "Lower" Then '小写字母
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsLower(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
ElseIf type.ToString = "Upper" Then '大写字母
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsUpper(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
ElseIf type.ToString = "All" Then '所有大小写字母
'可以直接调用上面的函数实现
'Return ReturnStr(str)
'使用以下代码,熟悉各个Char的用法
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsLetter(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
Else
Return "Type Select Error,Please Select in Lower,Upper and All"
End If
End Function
#End Region
End Class
读取数字串#Region "读取数字串"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnNumber()Function ReturnNumber(ByVal str As String) As Double 'Integer 太小,可容纳的数字量少
'返回值:
' -1:表示数字0
' -2:表示字符为空
' -3:表示有一个字符,且不是数字
Dim i As Integer = str.Length
Dim j As Integer
Dim numstr As String = str.Substring(0, 0)
Dim checkstr As String
If i = 0 Then
Return -2 ' -2:表示字符为空
End If
If str <> "0" Then
If i = 1 Then
If Char.IsNumber(str) = False Then
Return -3 ' -3:表示有一个字符,且不是数字
Else
Return Convert.ToInt32(str)
End If
ElseIf i >= 2 Then
For j = 0 To i - 1
checkstr = str.Substring(j, 1)
If Char.IsNumber(checkstr) = True Then
numstr += checkstr
End If
Next
If numstr.Length <> 0 Then
Return Convert.ToDouble(numstr)
Else
Return -1
End If
End If
Else '表示字符串为 "0"
Return -1 ' -1:表示数字0
End If
End Function
#End Region
读取字符串#Region "读取字符串"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnStr()Function ReturnStr(ByVal str As String) As String
'返回数字0表示字符串为空
'否则返回所有字符串.其他字符过滤
If str.Length = 0 Then
Return "0"
End If
Dim rtnStr As String = ""
Dim chkStr As String
Dim i As Integer = str.Length
Dim j As Integer
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
'不是数字,空格,符号
If Char.IsNumber(chkStr) = False And Char.IsWhiteSpace(chkStr) = False And Char.IsPunctuation(chkStr) = False And Char.IsSymbol(chkStr) = False Then
rtnStr += chkStr
End If
Next
If rtnStr.Length <> 0 Then
Return rtnStr
Else
Return "0"
End If
End Function
#End Region
读取符号#Region "读取符号"
/**/''' <summary>
'''
''' </summary>
''' <param name="str"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnSymbol()Function ReturnSymbol(ByVal str As String) As String
If str.Length = 0 Then
Return "0" '字符串空
End If
Dim rtnStr As String = ""
Dim chkStr As String
Dim i As Integer = str.Length
Dim j As Integer
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsNumber(chkStr) = False And Char.IsWhiteSpace(chkStr) = False And Char.IsLetter(chkStr) = False Then
rtnStr += chkStr
End If
Next
If rtnStr.Length <> 0 Then
Return rtnStr
Else
Return "0"
End If
End Function
#End Region
按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串#Region "按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串"
/**/''' <summary>
''' 按照Lower(小写字母),Upper(大写字母),All(所有字母)读取字符串
''' </summary>
''' <param name="str"></param>
''' <param name="type">类型:Lower(小写字母),Upper(大写字母),All(所有字母)</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ReturnChar()Function ReturnChar(ByVal str As String, ByVal type As String) As String
Dim chkStr As String
Dim rtnStr As String = ""
Dim i As Integer = str.Length
Dim j As Integer
If i = 0 Then
Return "String Input Error,Please check again."
End If
If type.ToString = "Lower" Then '小写字母
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsLower(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
ElseIf type.ToString = "Upper" Then '大写字母
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsUpper(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
ElseIf type.ToString = "All" Then '所有大小写字母
'可以直接调用上面的函数实现
'Return ReturnStr(str)
'使用以下代码,熟悉各个Char的用法
For j = 0 To i - 1
chkStr = str.Substring(j, 1)
If Char.IsLetter(chkStr) = True Then
rtnStr += chkStr
End If
Next
Return rtnStr
Else
Return "Type Select Error,Please Select in Lower,Upper and All"
End If
End Function
#End Region
End Class
窗体代码
Imports ClassVB.CheckInput
Public Class Form1Class Form1
Private Sub TextBox2_TextChanged()Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub TextBox1_TextChanged()Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim str As String = TextBox1.Text.ToString
' TextBox2.Text = Convert.ToString(ReturnNumber(str))
TextBox2.Text = ReturnNumber(str).ToString
TextBox3.Text = ReturnStr(str)
TextBox4.Text = ReturnSymbol(str)
TextBox5.Text = ReturnChar(str, "Lower")
TextBox6.Text = ReturnChar(str, "Upper")
End Sub
End Class
Public Class Form1Class Form1
Private Sub TextBox2_TextChanged()Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub TextBox1_TextChanged()Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim str As String = TextBox1.Text.ToString
' TextBox2.Text = Convert.ToString(ReturnNumber(str))
TextBox2.Text = ReturnNumber(str).ToString
TextBox3.Text = ReturnStr(str)
TextBox4.Text = ReturnSymbol(str)
TextBox5.Text = ReturnChar(str, "Lower")
TextBox6.Text = ReturnChar(str, "Upper")
End Sub
End Class
程序界面