Results 1 to 2 of 2

Thread: Stupid Problem, with Replace

  1. #1

    Thread Starter
    Lively Member tHa-gRiM's Avatar
    Join Date
    Sep 2005
    Posts
    126

    Stupid Problem, with Replace

    Im taking a VB class and cant figure out how to get it to replace more than one character this is my code.



    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class JacobsonForm
    5.     Inherits System.Windows.Forms.Form
    6.  
    7.     Private Sub uiExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uiExitButton.Click
    8.         'ends the application
    9.         Me.Close()
    10.     End Sub
    11.  
    12.     Private Sub uiDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiDisplayButton.Click
    13.         Dim oldpass As String = Me.uiOldPassTextBox.Text
    14.         Dim oldpassnumber As Double
    15.         Dim newpass As String
    16.         Dim i As Integer = 0
    17.  
    18.  
    19.         oldpassnumber = oldpass.Length
    20.         If oldpassnumber >= 5 AndAlso oldpassnumber < 8 Then
    21.  
    22.             Do Until i = oldpassnumber
    23.                 newpass = oldpass.Replace("A", "X")
    24.                 newpass = oldpass.Replace("E", "X")
    25.                 newpass = oldpass.Replace("I", "X")
    26.                 newpass = oldpass.Replace("O", "X")
    27.                 newpass = oldpass.Replace("U", "X")
    28.                 i = i + 1
    29.             Loop
    30.         End If
    31.  
    32.         Me.uiNewPassLabel.Text = newpass
    33.  
    34.  
    35.  
    36.  
    37.     End Sub
    38. End Class


    what am i missing i tried to get it to loop, but it only does the U, how can i get to do them all?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Stupid Problem, with Replace

    I don't see why you are looping at all. Calling .Replace will replace all instances of the character in the string, so why do the loop?

    In regards to your question about it only replacing one of them, it is because you are calling replace on the original oldpass string, and not changing the oldpass string at all, so it replaces the character each time, but then on the next line, it goes back to the original pass and then just switches one character, so it will only show the last replace call. To fix it, just assign the replace to the same "oldpass" variable, and that should do it, without having to loop at all.
    VB Code:
    1. Dim oldpass As String = "XAXEXIXOXU"
    2.         oldpass = oldpass.Replace("A", "X")
    3.         oldpass = oldpass.Replace("E", "X")
    4.         oldpass = oldpass.Replace("I", "X")
    5.         oldpass = oldpass.Replace("O", "X")
    6.         oldpass = oldpass.Replace("U", "X")
    7.         MessageBox.Show(oldpass)
    If you set a breakpoint in your code above that was giving you problems and step line by line, or place a messagebox in between each call displaying the newpass variable, you will see what was going on.
    Last edited by gigemboy; Oct 28th, 2006 at 07:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width