Replacing text at runtime

Introduction

Replacing text at runtime is a simple task when using the replace function.
It can replace a letter, word, or selected letters with any type of string.
Please note that this will only work in VB 6 and higher.

Private Sub SpellCheck_Click()
     'This sub can be put in any type of
     'form with 2 Text boxes called Text1 and Text2
     'and a command button called SpellCheck
     Dim Temp As String
     'I prefer to put the replaced text into a variable

     Temp = Text1.Text
     'Assume you want to edit Text1.Text

     Temp = Replace(Temp, "arent", "aren't")
     'This would replace arent with aren't
     'It could also be: Temp = Replace (Temp, "arent", "aren't", 10, 1)
     'This would start searching from the 10th character and would stop after replacing one mistake

     Text2.Text = Temp
     'You then reassign the string to a textbox

End Sub


Addition below this line is made by the moderator.
Here is an example how to do this in a public module.

Assumed you are replacing all occurances of "arent" in the string with "aren't"

     YourVariable = strSpellCheck("The string you arent pleased with", "arent", "aren't")

'this returns

     YourVariable = "The string you aren't pleased with"


Public Function strSpellCheck(ByVal FullText As String, _
ByVal strReplace As String, _
ByVal strReplaceWith) As String

'This function can be put in any type of module or form code

     Dim Temp As String
'I prefer to put the replaced text into a variable      Temp = FullText
'assume you want to edit Text1.Text

     Temp = Replace(Temp, strReplace, strReplaceWith)

'This would replace arent with aren't
'It could also be: Temp = Replace (Temp, "arent", "aren't", 10, 1)
'This would start searching from the 10th character and would stop after replacing one mistake

'You then reassign the string to the function.

     strSpellCheck = Temp

End Function

/html>

You might also like...

Comments

 ZERO-COOL I'm a 12 year old child who is interested in learning programming languages. My current two are Visual Basic and C++.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger