ViRuaL 0 Report post Posted May 12, 2005 This tutorial will go over how to effectively use the Replace function in Visual Basic. 1. So you have a string. Whether it be a label caption, an entry in a text box by the user of your program, or an item in a combo box. Let's say this is a bad string, something you do not want to appear. For instance, a user types "X is gay" in a text box, and you want to change that to something else. You can simply have it be deleted, or replaced with another value. 2. To make this work when a user types something in a textbox, you will need to use this sub: Private Sub Text1_Change()End SubThen we'll use the replace function. This looks like: Replace(Expression, WhatToReplace, ReplaceWithWhat) 3. Now we want to find the text "X is gay" in the textbox. Remember our Text1_Change() Sub? We'll now insert the following code inside that sub. Private Sub Text1_Change()Dim txttxt = Text1.TextIf InStr(1, Text1.Text, "X is gay") > 0 ThenText1.Text = Replace(txt, "X is gay", "User is gay")End IfEnd Sub 4. Done! What this code does is look for the value "X is gay" in Text1.Text, and if that is greater than 0 (meaning true) it replaces text in Text1.Text with "User is gay" That's all there is to it. If you have any questions, feel free to e-mail me at ViRuaL@gmail.com. If you thought this was completely useless, feel free to flame it into oblivion. Share this post Link to post Share on other sites
miCRoSCoPiC^eaRthLinG 0 Report post Posted May 12, 2005 Good job Come back and post some more of such cool tips & tricks :(All the best,m^e Share this post Link to post Share on other sites
ViRuaL 0 Report post Posted May 12, 2005 Why thank you. :(I will be posting a lot of cool things I've learned... Looking forward to my free hosting . Anyway, off to write my Start Button Editor post... Completely original code that I think is very cool. Share this post Link to post Share on other sites
iGuest 3 Report post Posted September 27, 2007 I've been trying to figure this out for a while and this seems almost there... but when I add another "Remove" line only the top one works... any ideas? Share this post Link to post Share on other sites
iGuest 3 Report post Posted March 30, 2008 VB replacing strings... Visual Basic: Replace Explained! How to relplace an oldword with an new word??? -question by Sukanya Share this post Link to post Share on other sites
iGuest 3 Report post Posted April 25, 2009 @Sukanya Visual Basic: Replace Explained!Sukanya, to replace an odd word use the following: Dim txtRplWordRplWord = "Word" ' Word you want to look forReplaceM = "NewWord" ' What is going to be replaced withTxt = Text1.TextIf InStr(1, Text1.Text, rplWord) > 0 Then Text1.Text = Replace(txt, rplWord, "replaceM)End If This will search Text1 for 'Word' and if it exists, replace's it with 'NewWord'. -reply by Markeh Share this post Link to post Share on other sites
iGuest 3 Report post Posted September 30, 2009 Made better calculator with thisVisual Basic: Replace Explained!Thanks! I couldn't figure out how to get my calculator (that I made) to replace the answer from the last problem when you pressed a number. For example, If I pressed 9x9 and =, the screen would show 81. Then if I pressed 7, it would show 817. Now it starts a new problem -reply by Josh Share this post Link to post Share on other sites
iGuest 3 Report post Posted November 24, 2009 Replace function will replace escape sequence?Visual Basic: Replace Explained!Hi , I am using Replace(sStr, sNullStr, " ") to replace a spaces.But after al the spaces are replaced "e" is appended with my resultant string.Anybody can help me out. -question by janu Share this post Link to post Share on other sites