简体   繁体   中英

How can I check if a string contains specific charcaters in VB.net?

So, I did some research but didn't come up with any answers. I read about the Regex method, but I'm practically new in this and I have never heard of it.

What I'm trying to do is to identify, whether the user entered a password (in my case I call it "Student Number") that must only contain an uppercase letter S, eight numbers after the uppercase S, and finally a special character * (specifically in that order).

I already programmed this:

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim InvalidName As Integer = 0
        Dim InvalidStudentNumber_Numeric As Integer = 0

        For intIndex = 0 To txtName.Text.Length - 1
            If IsNumeric(txtName.Text.Substring(intIndex, 1)) Then
                InvalidName = 1
            End If
        Next

        For intIndex = 0 To txtStudentNumber.Text.Length - 1
            If IsNumeric(txtStudentNumber.Text.Substring(intIndex, 1)) Then
                InvalidStudentNumber_Numeric += 1

            End If
        Next

        If InvalidName <> 0 Then
            MessageBox.Show("The name entered does not meet the characters criteria.  Provide a non-numeric name, 10 characters or longer.",
                            "Invalid Information: Name")
            txtName.Focus()

        ElseIf InvalidStudentNumber_Numeric <> 8 Then
            MessageBox.Show("The student number entered does not meet the characters criteria.  Provide a non-numeric student number, 10 characters long.",
                            "Invalid Information: Student Number")
            txtStudentNumber.Focus()

So, as for the Student's Name I have no problems, but the password is what gets me. I already figured out how to know if it has numbers (it must have 8), but I don't know how to search for the uppercase S at the beginning and for the * at the end of the string.

No need for regex.

Public Function IsValidStudentNumber(ByVal id As String) As Boolean
    '  Note that the `S` and the `*` appear to be common to all student numbers, according to your definition, so you could choose to not have the users enter them if you wanted.
    Dim number As Int32 = 0

    id = id.ToUpper

    If id.StartsWith("S") Then
        ' Strip the S, we don't need it.
        ' Or reverse the comparison (not starts with S), if you want to throw an error.
        id = id.Substring(1)
    End If

    If id.EndsWith("*") Then
        ' Strip the *, we don't need it.
        ' Or reverse the comparison (not ends with *), if you want to throw an error.
        id = id.Substring(0, id.Length - 1)
    End If

    If 8 = id.Length Then
        ' Its the right length, now see if its a number.
        If Int32.TryParse(id, number) Then
            Return True
        End If
    End If
    Return False
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM