We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 13,226 times

Related Categories

Replacing text at runtime - Introduction

ZERO-COOL

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>

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

Comments