Changes

Working with Strings in Visual Basic

1,064 bytes added, 14:52, 2 August 2007
Trimming Leading and Trailing Spaces from a String
strLastName = Trim ( Mid( StrFullName, Instr(strFullName, " ")) )
</pre>
 
== Replacing Text in String ==
 
Visual Basic provide the ''Replace()'' function for the purposes of replacing some text in a string with some other text. The syntax for the ''Replace()'' function is as follows:
 
'''Replace(''' ''string'', ''searchtext'', ''replacetext'' ''')'''
 
''string'' represents the string in which to perform the search and replace. ''searchtext'' identifies the text which is to be replaced. ''replacetext'' represents the text with which the located text is to replaced.
 
The following Visual Basic code excerpt will replace the word "Paris" with the word "London":
 
<pre>
Dim strMyText As String = "Visit Paris Today"
Dim strNewtext As String
 
strNewText = Replace ( strMyText, "Paris", "London")
</pre>
 
Note that the ''Replace()'' function will replace all instances of the substring with the new string. For example, the following code will result in a string which reads "Visit London Today. Flights to London are cheaper":
 
<pre>
strNewText = Replace ( "Visit Paris Today. Flights to Paris are Cheaper", "Paris", "London" )
</pre>