Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 5th, 2005, 02:26 PM
baseballdude_'s Avatar
baseballdude_ baseballdude_ is offline
Expert Learner
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2005
Location: Wisconsin
Posts: 1,891 baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)  Folding Points: 22104 Folding Title: Starter FolderFolding Points: 22104 Folding Title: Starter Folder
Time spent in forums: 1 Week 5 Days 13 h 45 m 58 sec
Reputation Power: 70
Send a message via AIM to baseballdude_ Send a message via MSN to baseballdude_ Send a message via Yahoo to baseballdude_ Send a message via Google Talk to baseballdude_
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

Reply With Quote
  #2  
Old October 5th, 2005, 07:27 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 29 m 58 sec
Reputation Power: 181
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

Reply With Quote
  #3  
Old October 6th, 2005, 08:09 AM
baseballdude_'s Avatar
baseballdude_ baseballdude_ is offline
Expert Learner
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2005
Location: Wisconsin
Posts: 1,891 baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)baseballdude_ User rank is Second Lieutenant (5000 - 10000 Reputation Level)  Folding Points: 22104 Folding Title: Starter FolderFolding Points: 22104 Folding Title: Starter Folder
Time spent in forums: 1 Week 5 Days 13 h 45 m 58 sec
Reputation Power: 70
Send a message via AIM to baseballdude_ Send a message via MSN to baseballdude_ Send a message via Yahoo to baseballdude_ Send a message via Google Talk to baseballdude_
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.

Reply With Quote
  #4  
Old October 6th, 2005, 10:45 AM
Darius Darius is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 108 Darius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 54 sec
Reputation Power: 5
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


Reply With Quote
  #5  
Old October 6th, 2005, 02:23 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 29 m 58 sec
Reputation Power: 181
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.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Infinite Loop Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT