-
Oct 15th, 2015, 05:37 AM
#1
Thread Starter
Hyperactive Member
[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
-
Oct 15th, 2015, 06:17 AM
#2
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)
-
Oct 15th, 2015, 09:05 AM
#3
Thread Starter
Hyperactive Member
Re: Need help: replacing strings
Originally Posted by Bonnie West
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.
-
Oct 15th, 2015, 09:32 AM
#4
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.
-
Oct 15th, 2015, 09:37 AM
#5
Thread Starter
Hyperactive Member
Re: Need help: replacing strings
I have one more question... What is the purpose of & after 3?
-
Oct 15th, 2015, 10:36 AM
#6
Re: Need help: replacing strings
-
Oct 15th, 2015, 04:47 PM
#7
Fanatic Member
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
-
Oct 15th, 2015, 05:52 PM
#8
Re: Need help: replacing strings
-
Oct 16th, 2015, 12:20 PM
#9
Fanatic Member
Re: [RESOLVED] Need help: replacing strings
Function which does what OP asked.
txtResult.Text = RemoveLast("James / Lars / Robert / Kurt / need to eliminate this", "/")
-
Oct 16th, 2015, 01:38 PM
#10
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.
-
Oct 16th, 2015, 02:01 PM
#11
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.
-
Oct 16th, 2015, 03:27 PM
#12
Fanatic Member
Re: [RESOLVED] Need help: replacing strings
Originally Posted by MMock
...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.
-
Oct 16th, 2015, 11:28 PM
#13
Re: [RESOLVED] Need help: replacing strings
Originally Posted by MMock
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|