6 Replies - 764 Views - Last Post: 23 March 2013 - 01:42 AM Rate Topic: -----

#1 dnalvlc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Replacing characters using .replace

Posted 22 March 2013 - 02:01 PM

Ok I am supposed to make this program use between 5 and 7 characters for a password and then change the first vowel to the number 1, the second vowel to the number 2, and all other vowels to the number 9. After that I am supposed to make any numbers entered for the password change to a Z. I have tried looking up the .replace and substring functions, but I am just so lost. So far I can only seem to make it repeat what is in the input box and reverse it. Any help would be great.

Here is what I have so far and I am stuck.

 Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Close()
    End Sub

    Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim strOriginal As String = txtOriginal.Text
        Dim strnew As String
        Dim valid As Boolean



        If strOriginal.Length >= 5 And strOriginal.Length <= 7 Then
            valid = True
        Else
            valid = False
            MessageBox.Show("Invalid Password. Please enter 5-7 characters")
        End If

        Dim charIndex As Integer = strOriginal.IndexOf(0)

        If valid = True Then
            strnew = strOriginal.Replace(0, 1)
            strnew = strOriginal.Replace(1, 2)
            strnew = strOriginal.Replace(2, 9)
            strnew = strOriginal.Replace(3, 9)
            strnew = strOriginal.Replace(4, 9)
            strnew = strOriginal.Replace(5, 9)
            strnew = strOriginal.Replace(6, 9)

            lblNew.Text = strnew

        ElseIf valid = True Then
            strnew = strOriginal.Replace(0, "Z")
            strnew = strOriginal.Replace(1, "Z")
            strnew = strOriginal.Replace(2, "Z")
            strnew = strOriginal.Replace(3, "Z")
            strnew = strOriginal.Replace(4, "Z")
            strnew = strOriginal.Replace(5, "Z")
            strnew = strOriginal.Replace(6, "Z")

            lblNew.Text = strnew


        ElseIf valid = False Then
            lblNew.Text = String.Empty

        End If



    End Sub
End Class 


I have also tried using
 strOriginal Like "[AEIOU]" Then
            strnew = strOriginal.Replace(0, 1)
            strnew = strOriginal.Replace(1, 2)
            strnew = strOriginal.Replace(2, 9)
            strnew = strOriginal.Replace(3, 9)
            strnew = strOriginal.Replace(4, 9)
            strnew = strOriginal.Replace(5, 9)
            strnew = strOriginal.Replace(6, 9) 
but I still cant figure out why it is not working.

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing characters using .replace

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: Replacing characters using .replace

Posted 22 March 2013 - 02:14 PM

Strings are immutable in .net, so method anything you do on a string returns a new instance. Thus you not changing the original.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,409
  • Joined: 12-December 12

Re: Replacing characters using .replace

Posted 22 March 2013 - 02:29 PM

You should learn to debug, and step-through, your code - there is a tutorial link in my signature.

You should also have Option Strict On so that you can see any error or warning messages. In particular, Replace() is to replace a string with a string, or a character with a character, but you are attempting to replace numbers (0..9); you should be replacing "0", "1", etc..

The following code is based on your use of LIKE, as there are other ways to do this:

        Dim strPassword As String = "PasswordEE1"
        Dim strAlteredP As String = String.Empty
        Dim strVowelNo As String = "1"
        For Each ch In strPassword
            If ch.ToString.ToUpper Like "[AEIOU]" Then
                strAlteredP &= strVowelNo
                If strVowelNo.Equals("1") Then
                    strVowelNo = "2"
                Else
                    strVowelNo = "9"
                End If
            Else
                strAlteredP &= ch
            End If
        Next

You should study this and you'll need to extend it to also replace numbers with "Z".

Result: P1ssw2rd991
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,409
  • Joined: 12-December 12

Re: Replacing characters using .replace

Posted 22 March 2013 - 02:45 PM

I would prefer to work explicitly with characters though:

        Dim strPassword As String = "PasswordEE1"
        Dim strAlteredP As String = String.Empty
        Dim chVowelNo As Char = "1"c
        For Each ch As Char In strPassword
            If ch.ToString.ToUpper Like "[AEIOU]" Then
                strAlteredP &= chVowelNo
                If chVowelNo.Equals("1"c) Then
                    chVowelNo = "2"c
                Else
                    chVowelNo = "9"c
                End If
            Else
                strAlteredP &= ch
            End If
        Next

You can concentrate on either version though - this one is just a minor technical change.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,480
  • Joined: 29-May 08

Re: Replacing characters using .replace

Posted 22 March 2013 - 03:11 PM

The mutable version of the string is the Text.StringBuilder.
If you call the .ToString method you get an immutable string.

This post has been edited by AdamSpeight2008: 22 March 2013 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 dnalvlc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-March 13

Re: Replacing characters using .replace

Posted 22 March 2013 - 04:26 PM

View Postandrewsw, on 22 March 2013 - 02:29 PM, said:

You should learn to debug, and step-through, your code - there is a tutorial link in my signature.

You should also have Option Strict On so that you can see any error or warning messages. In particular, Replace() is to replace a string with a string, or a character with a character, but you are attempting to replace numbers (0..9); you should be replacing "0", "1", etc..

The following code is based on your use of LIKE, as there are other ways to do this:

        Dim strPassword As String = "PasswordEE1"
        Dim strAlteredP As String = String.Empty
        Dim strVowelNo As String = "1"
        For Each ch In strPassword
            If ch.ToString.ToUpper Like "[AEIOU]" Then
                strAlteredP &= strVowelNo
                If strVowelNo.Equals("1") Then
                    strVowelNo = "2"
                Else
                    strVowelNo = "9"
                End If
            Else
                strAlteredP &= ch
            End If
        Next

You should study this and you'll need to extend it to also replace numbers with "Z".

Result: P1ssw2rd991

Alright I got this part to working with my program with what the user types into the textbox.I forgot to mention it in the initial post, but I am also supposed to reverse the output which I managed to do by using
 lblNew.Text = StrReverse(strAltered) 


Now in order to get it to read numbers and make them Z I tried to make another for loop, but this is not working and it prints the output twice in my label. I am new to the for each loop.

 Dim strNum As String = "Z"

        For Each ch In strPassword
            If ch.ToString.ToUpper Like "[0123456789]" Then
                strAltered &= strNum
        lblNew.Text = StrReverse(strAltered)


I apologize if these are tiring simple questions for you guys, but its really helping me learn this better than I can figure out in the book I have.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,409
  • Joined: 12-December 12

Re: Replacing characters using .replace

Posted 23 March 2013 - 01:42 AM

If you have just added your recent code after the code that I presented then, yes, it will just add further characters to the result. This tells me that you haven't studied the code. You should learn to debug code, then you can step-through it and discover how mine works.

I would modify my code to incorporate:

ElseIf ch.ToString.ToUpper Like "[0-9]" Then

but you'll have to work out how to incorporate this yourself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1