Results 1 to 13 of 13

Thread: [RESOLVED] Need help: replacing strings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    328

    Resolved [RESOLVED] Need help: replacing strings

    I turn spaces into "/", and now my problem is how do I eliminate excess "/" on the last part?

    Heres my code:
    Code:
    Dim sNames As String
        
        sNames = IIf((Len(Trim(txtName(0).Text)) = 0), "", txtName(0).Text & " ") _
                & IIf((Len(Trim(txtName(1).Text)) = 0), "", txtName(1).Text & " ") _
                & IIf((Len(Trim(txtName(2).Text)) = 0), "", txtName(2).Text & " ") _
                & IIf((Len(Trim(txtName(3).Text)) = 0), "", txtName(3).Text & " ")
        
    txtResult.Text = Replace(sNames, " ", " / ") ' James / Lars / Robert / Kurt / need to eliminate this.
    What if txtName(3).Text equals to blank? Result will be: James / Lars / Robert /


    Result I am looking for : James / Lars / Robert / Kurt

    thanks

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Need help: replacing strings

    Try this:

    Code:
    Private Sub txtName_Change(Index As Integer)
        Dim i As Integer, sName As String, sResult As String
    
        For i = 0 To 3
            sName = Trim$(txtName(i).Text)
            If LenB(sName) Then sResult = sResult & (sName & " / ")
        Next
    
        If LenB(sResult) Then
            txtResult.Text = Left$(sResult, Len(sResult) - 3&)
        Else
            txtResult.Text = vbNullString
        End If
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    328

    Re: Need help: replacing strings

    Quote Originally Posted by Bonnie West View Post
    Try this:

    Code:
    Private Sub txtName_Change(Index As Integer)
        Dim i As Integer, sName As String, sResult As String
    
        For i = 0 To 3
            sName = Trim$(txtName(i).Text)
            If LenB(sName) Then sResult = sResult & (sName & " / ")
        Next
    
        If LenB(sResult) Then
            txtResult.Text = Left$(sResult, Len(sResult) - 3&)
        Else
            txtResult.Text = vbNullString
        End If
    End Sub


    Thank you so much Bonnie West... This is exactly what I need.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Need help: replacing strings

    Please mark this (and any other) thread as Resolved if you are satisfied with it. Also, don't forget to 'rate this post' for the person(s) who assisted in the resolution of the thread.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    328

    Re: Need help: replacing strings

    I have one more question... What is the purpose of & after 3?

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Need help: replacing strings


  7. #7
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: Need help: replacing strings

    Debug.Print RemoveLast("James / Lars / Robert / Kurt / need to eliminate this", "/")

    Code:
    Function RemoveLast(ByVal sSource As String, ByVal sRemove As String) As String
    Dim lPos As Long
    lPos = InStrRev(sSource, sRemove)
    If lPos Then
        RemoveLast = Trim(Left(sSource, lPos - 1))
    Else
        RemoveLast = sSource
    End If
    End Function

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Need help: replacing strings

    Tech...what is that?

  9. #9
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: [RESOLVED] Need help: replacing strings

    Function which does what OP asked.

    txtResult.Text = RemoveLast("James / Lars / Robert / Kurt / need to eliminate this", "/")

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: [RESOLVED] Need help: replacing strings

    Oh...I thought it was about the second question (&)....as OP already stated he had 'exactly what he' needed, (but then asked another question later), I thought you were somehow giving him something about that....that is all. Sorry.

  11. #11
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: [RESOLVED] Need help: replacing strings

    Why do you want 3 to be a Long?

    Also, I've noticed there is a habit occasionally of people posting how they would've solved a problem, even after someone has already posted how to solve the problem. Maybe Tech could've prefaced the post with "Alternatively, you can..." or "My two cents:..."
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  12. #12
    Fanatic Member
    Join Date
    Apr 2015
    Location
    Finland
    Posts
    679

    Re: [RESOLVED] Need help: replacing strings

    Quote Originally Posted by MMock View Post
    ...Maybe Tech could've prefaced the post with "Alternatively, you can..." or "My two cents:..."
    Yes - exactly, it was meant to introduce an alternative solution. Works even if the item count changes.

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] Need help: replacing strings

    Quote Originally Posted by MMock View Post
    Why do you want 3 to be a Long?
    Because the Length parameter of the Left$ function is a Long and the Len function also returns a Long.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

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