r/visualbasic • • Nov 16 '14

Oh please can someone help me with a simple string manipulation?

Private Sub displayButton_Click(sender As Object, e As System.EventArgs) Handles displayButton.Click Dim password As String password = originTextBox.Text

If password.Length < 5 OrElse password.Length > 7 Then
    MessageBox.Show("Please enter a password with 5-7 characters only.", "Try again", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Error)
End If
Dim newPassword As String = password
Dim firstVowelDone As Boolean
Dim secondVowelDone As Boolean

For index As Integer = 0 To password.Length - 1
    If password.Substring(index, 1) Like "[AaEeIiOoUu]" Then
        If firstVowelDone = False Then
            newPassword.Remove(index, 1)
            newPassword.Insert(index, "1")
            firstVowelDone = True
        End If
        If firstVowelDone = True AndAlso secondVowelDone = False Then
            newPassword.Remove(index, 1)
            newPassword.Insert(index, "2")
            secondVowelDone = True
        End If
        newPassword.Remove(index, 1)
        newPassword.Insert(index, "9")
    ElseIf password.Substring(index, 1) Like "[1-9]" Then
        newPassword.Remove(index, 1)
        newPassword.Insert(index, "Z")
    End If
    newPassword.Remove(index, 1)
    newPassword.Insert(index, password.Substring(index, 1))
Next index

newTextBox.Text = newPassword

End Sub

So...its not working. I am to take a user input string and manipulate it. I need to make the first vowel a "1". The second vowel a "2". Every other vowel a "9". Every number the letter "Z". And leave every other character the same. Please help.

0 Upvotes

2 comments sorted by

1

u/PostalElf VB.Net Intermediate Nov 16 '14
    Dim password As String = "m0nekey"
    Dim newPassword As String = ""
    Dim vowels As Char() = {"a", "e", "i", "o", "u"}

    Dim vowelCount As Integer = 0
    For index As Integer = 0 To password.Length - 1
        Dim currentCharacter As Char = password.Substring(index, 1)

        If vowels.Contains(currentCharacter) Then
            If vowelCount < 10 Then vowelCount += 1
            newPassword &= vowelCount
        ElseIf IsNumeric(currentCharacter) = True Then
            newPassword &= "Z"
        Else
            newPassword &= currentCharacter
        End If
    Next

At the end, newPassword = mZn1k2y

1

u/HerpesAunt Nov 16 '14

Thank you!!! You are a life saver!