Changes

Working with Strings in Visual Basic

1,561 bytes added, 14:05, 2 August 2007
Extracting Text from the End of a String
== Extracting Text from the End of a String ==
 
Text may be extracted from the end of a string using the Visual Basic ''Microsoft.VisualBasic.Right()'' function. This function accepts two parameters. The first parameter is either a string literal, or a string variable from which the text is to be extracted. The second parameter tells the function how many characters back from the end of the string to begin the extraction. For example, the following code excerpt will result in the word "Spring" being extracted from the string and assigned to the ''strLastWord'' variable:
 
<pre>
Dim strLastWord As String
 
strLastWord = Microsoft.VisualBasic.Right("Paris in the Spring", 6)
</pre>
 
== Extracting Text from Anywhere in a String ==
 
So far we have looked at extracting text from the beginning and end of a string. Visual Basic also provides a function for extracting any any number of characters from any position in a string. This is achieved using the Visual Basic ''Mid()'' function.
 
The ''Mid()'' function accepts two mandatory and one optional parameter as follows:
 
* The string literal or variable from which the text is to be extracted
 
* The starting character position in the string from which the text extraction should begin
 
* Optionally, the number of characters to extract. If omitted all characters from the start position to the end of the string are extracted.
 
The following Visual Basic code excerpt extracts the word "Paris" from the string literal and assigns it to the strMidString variable:
 
<pre>
Dim strMidString As String
 
strMidString = Mid("Visit Paris today", 7, 5 )
</pre>