Jump to content
xisto Community
Sign in to follow this  
ViRuaL

Visual Basic: Replace Explained!

Recommended Posts

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 Sub
Then 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

Why thank you. :(I will be posting a lot of cool things I've learned... Looking forward to my free hosting :D. 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

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

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
@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
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 :D

-reply by Josh

Share this post


Link to post
Share on other sites
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.