|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Infinite Loop Help
This code is an assignment of mine in class. I have to create a program that acts as a "Find and Replace" type thing. How can I get my Replace() function to Loop over and over so that it replaces every letter, not just the first? I've tried a couple loops, but I can't figure out what to compare the loop with.
Code:
Function Replace(StringToModify, StringToFind, StringToReplaceWith) Dim strNewString As String Dim strBeforeFindLetter As String Dim strAfterFindLetter As String strBeforeFindLetter = Left(StringToModify, InStr(StringToModify, Left(StringToFind, 1)) - 1) strAfterFindLetter = Right(StringToModify, (Len(StringToModify) - InStr(StringToModify, Left(StringToFind, 1)))) strNewString = strBeforeFindLetter & Left(StringToReplaceWith, 1) & strAfterFindLetter Print (strNewString) Label4.Caption = strNewString End Function Private Sub Command1_Click() Label4.Caption = Replace(Text1.Text, Text2.Text, Text3.Text) End Sub Private Sub Command2_Click() Unload Form1 End Sub Private Sub Text1_Change() Label4.Caption = "Max length 20 chars!" End Sub
__________________
www.xoise.com - www.ourfreegames.com - www.g1games.com - www.randomtools.net - www.xenocide-rpg.com - Lyrics Search Engine |
|
#2
|
|||
|
|||
|
One problem is naming your function Replace, since VB has a built-in Replace function which will work on the entire string you pass it.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
||||
|
||||
|
I figured it did at first, and when I tried using it, it said function not defined, so I proceeded to making my own function. I'm using Visual Basic 5.
|
|
#4
|
|||
|
|||
|
You can use replace but it's a sub not a function, but you can create your own.
Code:
Function Replace1(ByVal StringToModify, StringToFind, StringToReplaceWith) As String
Dim strNewString As String
Dim strBeforeFindLetter As String
Dim strAfterFindLetter As String
strNewString = ""
POS = InStr(1, StringToModify, StringToFind)
While POS <> 0
strNewString = strNewString & Left(StringToModify, POS - 1) & StringToReplaceWith
StringToModify = Right(StringToModify, Len(StringToModify) - (POS - 1 + Len(StringToFind)))
POS = InStr(1, StringToModify, StringToFind)
Wend
Replace1 = strNewString
End Function
Private Sub Command1_Click()
Label4.Caption = Replace1(Text1.Text, Text2.Text, Text3.Text)
End Sub
Private Sub Text1_Change()
Label4.Caption = "Max length 20 chars!"
End Sub
![]() |
|
#5
|
|||
|
|||
|
VB5 I have no idea, sorry. Doesn't the online VB help show you all the available VB functions? BTW, Replace in VB6 is a function that returns the string after replacements are made.
|
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Infinite Loop Help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|