CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Modify string

  1. #1
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Question Modify string

    How would I go about modifing a string with code? Lets say I have a string '12345' and I want to change the 3 to a six. How could I accomplish this task. I know how to grab the 3 itself and change it as a variable and I know how to get the postion of the 3, but how would I change the string itself within the code? I am looking to pass the string as a value to the function I am working on. Any thoughts on how to do this?

    Thanks for any help or ideas given

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Modify string

    You could use replace()

    Syntax

    Replace(expression, find, replacewith[, start[, count[, compare]]])


    You could also use the Mid() statement

    Mid Statement Example
    This example uses the Mid statement to replace a specified number of characters in a string variable with characters from another string.

    Code:
    Dim MyString
    MyString = "The dog jumps"   ' Initialize string.
    Mid(MyString, 5, 3) = "fox"   ' MyString = "The fox jumps".
    Mid(MyString, 5) = "cow"   ' MyString = "The cow jumps".
    Mid(MyString, 5) = "cow jumped over"   ' MyString = "The cow jumpe".
    Mid(MyString, 5, 3) = "duck"   ' MyString = "The duc jumpe".

  3. #3
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    DataMiser,
    Thanks for the reply. The issue I have now is how to return the modified string back to the function that called it. Here is what I have currently, the function that calls, and the function that returns. When the function NumeralTest(RetVal) returns true it then calls the function ReplNum(RetVal). I am trying to modify the string 'RetVal', then return it to the calling function where it will then move through the other function to be evaluated. Here is what I have.

    Code:
    Public Function GetPassword() As String
    
    Dim RetVal As String
    Dim rgch As String
    Dim i As Integer
    Dim totalLength As Integer
    
      Randomize
    
        '***********************************************
        'Fills the string 'rgch' with the approved characters to
        'choose for the string 'RetVal'.
        '***********************************************
      rgch = "abcdefghijklmnopqrstuvwxyz"
      
      rgch = rgch + UCase(rgch) + "123456789" + "!#$*+,;:<=>?@^_|~"
      
        '***********************************************
        'Generates a random number from 15 to 32, to be
        'used for the password string length.
        '***********************************************
      totalLength = Int((32 - 15 + 1) * Rnd() + 15)
       
       For i = 1 To totalLength 'counter for the total string length
        
        '***********************************************
        'Fills the string 'RetVal' with the generated
        'alphanumeric string.
        '***********************************************
              
      RetVal = RetVal + Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
    
    Next
    '***************************************************
    'Create two functions to add missing values if returned FALSE.
    '***************************************************
        If Not NumeralTest(RetVal) Then
         Call ReplNum(RetVal)
         ElseIf Not SpecialChrTest(RetVal) Then
         'Do Something
          ElseIf Not AlphaChrTest(RetVal) Then
            'Do Something
           'ElseIf Not AcrNimTst(RetVal) Then
             'Do Something
         End If
          
    GetPassword = RetVal
    End Function
    And the called funtion
    Code:
    Private Function ReplNum(ByVal RetVal As String) As String
    Dim i, x, z, w, a, count As Integer
    Dim test, bustest As String
    
    '**********************************************
    'Numeral error section. Determines how many numerals _
    are needed to be either inserted or replaced within _
    the string 'RetVal'.
    '**********************************************
    z = 0
    test = "123456789"
    'w = InStr(RetVal, test)
    For i = 1 To Len(RetVal)
      bustest = Mid$(RetVal, i, 1)
       If InStr(test, bustest) Then
         count = count + 1
          End If
            Next
            
     If count = 0 Then
      Do While z < 2
       z = z + 1
        Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character/number value of 'x'
    
    Loop
      ElseIf count = 1 Then
       Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character/number value of 'x'
    End If
    Return RetVal
    End Function

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Modify string

    When you are calling a function you need to use a variable to hold the result. Instead of
    Code:
    Call ReplNum(RetVal)
    You would want to use something more like

    Code:
    RetVal = ReplNum(RetVal)

  5. #5
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    DataMiser,
    I tried what you suggested, but it is not returning the modified string. Any ideas as to what I am doing wrong? Here is what I have now, the sub, the calling function, and the called function:

    Code:
    Option Explicit
    Public Sub CmdBtn1_Click()
    Dim RepVal As String
        '***********************************************
        'Figure out how to make the textbox hidden
        'until 'password' has passed all the business rules
        'at which point, the textbox window will become
        'visable and a message will be thrown letting the
        'user know to use the shown password. Then the user
        'name and password will be copied to the data base
        'and the copy button will become active to allow the
        'user to copy and paste the password
        '************************************************
     
     
    RepVal = GetPassword
             
        TxtBx.Text = RepVal
    End Sub
    Code:
    Public Function GetPassword() As String
    
    Dim RetVal, RepVal As String
    Dim rgch As String
    Dim i As Integer
    Dim totalLength As Integer
    
      Randomize
    
        '***********************************************
        'Fills the string 'rgch' with the approved characters to
        'choose for the string 'RetVal'.
        '***********************************************
      rgch = "abcdefghijklmnopqrstuvwxyz"
      
      rgch = rgch + UCase(rgch) + "123456789" + "!#$*+,;:<=>?@^_|~"
      
        '***********************************************
        'Generates a random number from 15 to 32, to be
        'used for the password string length.
        '***********************************************
      totalLength = Int((32 - 15 + 1) * Rnd() + 15)
       
       For i = 1 To totalLength 'counter for the total string length
        
        '***********************************************
        'Fills the string 'RetVal' with the generated
        'alphanumeric string.
        '***********************************************
              
      RetVal = RetVal + Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
    
    Next
    '***************************************************
    'Create two functions to add missing values if returned FALSE.
    '***************************************************
        If Not NumeralTest(RetVal) Then
         RepVal = ReplNum(RetVal)
         'ElseIf Not SpecialChrTest(RetVal) Then
         'Do Something
          'ElseIf Not AlphaChrTest(RetVal) Then
            'Do Something
           'ElseIf Not AcrNimTst(RetVal) Then
             'Do Something
         End If
     
    
    End Function
    Code:
    Private Function ReplNum(ByVal RetVal As String) As String
    Dim i, x, z, w, a, count As Integer
    Dim test, bustest As String
    
    '**********************************************
    'Numeral error section. Determines how many numerals _
    are needed to be either inserted or replaced within _
    the string 'RetVal'.
    '**********************************************
    z = 0
    test = "123456789"
    'w = InStr(RetVal, test)
    For i = 1 To Len(RetVal)
      bustest = Mid$(RetVal, i, 1)
       If InStr(test, bustest) Then
         count = count + 1
          End If
            Next
            
     If count = 0 Then
      Do While z < 2
       z = z + 1
        Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character/number value of 'x'
    
    Loop
      ElseIf count = 1 Then
       Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character/number value of 'x'
    End If
    
    End Function

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Modify string

    You need to make sure that you code the function to return the password. That line is missing from your getpassword function

    as the last line of code prior to end function try
    Code:
    GetPassword=RetVal
    Looks like it is also missing from your other function now as well.


    Edit to add:
    Looking at the first post I see the line Return RetVal which apparently I mistook to be valid as this is the proper way in VB.Net but that line is now missing and the proper line was not added.
    Last edited by DataMiser; April 29th, 2009 at 02:47 PM.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Modify string

    Keep in mind that subs do not return a value. When you use a sub you can either just reference them by name or you can use the Call keyword.

    A function is designed to return a value of a specified type. When you create a function you must specify what type of value it will return.

    At some point inside the function this value must be assigned to the function for return.
    As a general rule all functions should always return a value even if that value is false, null or empty string.

    Code:
    Public MyFunction(PassMeData AS String) AS String
    
    Dim ResultString as String
    
    ' do some stuff here that will populate our resultstring
    
    MyFunction=ResultString
    End Function
    Anytime you call a function you should have a variable of the proper type to hold the returned data that results from the function call and then do whatever you need to do with that data.

    Code:
    Dim MyResultString as string
    Dim MYParamString as string
    
    MyResultString=MyFunction(MyParamString)
    MyResultString will contain the string that is returned from the function.
    Last edited by DataMiser; April 29th, 2009 at 02:48 PM.

  8. #8
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    DataMiser,
    Thank you for the help. It worked out perfectly. I was just missing the function returns like you suggested. Thanks a million.

  9. #9
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    Another question within my modify string function. If 'a' creates the position within the string 'RetVal' to be modified, how can I test to see if the position 'a' already has a numeral? The following lines creates a random number 'x' to replace position 'a' within the string 'RetVal'. Any thoughts??

    Code:
    'If 'a' equals the position of an existing numeral _
    then have it generate another position
      
    ElseIf count = 1 Then
       Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the value of 'x'
    End If

  10. #10
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Modify string

    Use can use the mid function and isnumeric() function for this or you could use the instr() function and mid() function.

    In the case of the latter you could do something like
    Code:
    if instr("0123456789",mid(MyString,a,1))>0 then
       'there is a numeral in that position
    else
       'the character there is not a numeral
    end if

  11. #11
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    Datamiser,
    Thank for your suggestion, it works great. In continuing with the modify string issue, the second function has to insert a special character. Here is my code for your review. Right now it is basiclly a copy of the numeral replace function. I am open to any suggestions you might have.

    Thanks

    Code:
    Private Function ReplSpc(ByVal RetVal As String) As String
    Dim i, x, z, a, count As Integer
    Dim bustest As String
    
    '**********************************************
    'Special Character error section. Determines how many _
    special characters are needed to be either inserted _
    or replaced within the string 'RetVal'.
    '**********************************************
    'Choose a random number from a pool of numbers? _
    The pool consists of 35, 36, 42-46, 58-64, 94, 95, 124, 126
    Const test As String = "!#$*+,;:<=>?@^_|~"
    
    For i = 1 To Len(RetVal)
      bustest = Mid$(RetVal, i, 1)
       If InStr(test, bustest) Then
         count = count + 1
          End If
            Next
            
     If count = 0 Then
       z = 0
      Do While z < 2
       z = z + 1
        Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character 'x'
    
    Loop
    'If 'a' equals the position of an existing numeral _
    then have it generate another position
      ElseIf count = 1 Then
       Randomize
         a = Int((Len(RetVal) - 1 + 1) * Rnd() + 1)
          x = Int((9 - 1 + 1) * Rnd() + 1) 'Produces a _
    random number between 1 and 9
    
           Mid(RetVal, a, 1) = x 'Replaces the character at _
    position 'a' with the character 'x'
    End If
    ReplSpc = RetVal
    End Function

  12. #12
    Join Date
    Mar 2003
    Location
    Crofton, MD
    Posts
    76

    Re: Modify string

    DataMiser,
    Never mind I figured it out myself.

    Thanks, anyway

    Code:
    Const test2 As String = "!#$*+,;:<=>?@^_|~"
    
    x = Mid$(test2, Int(Rnd() * Len(test2) + 1), 1)

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

Featured