Difference between revisions of "Working with Strings in Visual Basic"

From Techotopia
Jump to: navigation, search
(Extracting Text from the Beginning of String)
(Extracting Text from the End of a String)
Line 71: Line 71:
  
 
== 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>

Revision as of 14:05, 2 August 2007

In teh real world, humans communicate usign words and sentences. Given that most computer applications need to interact with humans it is not surprising that the average Visual Basic programmer spends a lot of time dealing with words and sentences in the form of String when developing applications.

In this chapter we will explore the subject of manipulating strings in Visual Basic.


Contents


Concatenating Strings in Visual Basic

The process of combining two strings together to from one string is called concatenation. Strings are concatenated in Visual Basic using the ampersand (&) operator. For example, the following Visual Basic code sample combines three strings together to create a single string, which is assigned to a third string variable:

Dim strFirstName As String = "Fred"
Dim strLastName As String = "Smith"
Dim strTitle As String = 'Mr."
Dim strSalutation As String

strSalutation = strTitle & " " & strFirstName & " " & strLastName

Note that the above example also puts space characters between each string. The above code, therefore, will assign the following string to the strSalutation variable:

Mr. Fred Smith

String concatenation does not have to be limited to variables. String literals may also be included in a concatenation. String literals are strings of text placed directly into quotes. For example the following variation of the above example creates a string which reads "Hello Mr. Fred Smith":

Dim strFirstName As String = "Fred"
Dim strLastName As String = "Smith"
Dim strTitle As String = 'Mr."
Dim strSalutation As String

strSalutation = "Hello" & strTitle & " " & strFirstName & " " & strLastName

Finding the Length of a String

it is common to need to find out the number of characters in any given string. Visual Basic provides mechanisms for finding the length of both string variables and string literals. The length of a string literal can be obtained using the Len() function, which takes the string as an argument and returns the length:

Dim decLength As Decimal

decLength = Len("This is a string literal")

This technique works equally well when dealing with string variables:

Dim decLength As Decimal
Dim strMyString As String = "This is a String Variable Value"

decLength = Len(strMyString)

It is importsant to understand that a String variable is really an object which has properties and methods like any other object. One such method is the Length() method which can also be used to obtain the length of a string variable:

Dim decLength As Decimal
Dim strMyString As String = "This is a String Variable Value"

decLength = strMyString.Length()

Extracting Text from the Beginning of String

A portion from the beginning of a string can be extracted using the Microsoft.VisualBasic.Left() function. This function takes two paramaters, the string from which the text is to extracted and the number of characters to extract. For example, the following code extracts the word "This" from the string:

Dim strMyString As String = "This is a String Variable Value"

Microsoft.VisualBasic.Left(strMyString, 4)

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:

Dim strLastWord As String

strLastWord = Microsoft.VisualBasic.Right("Paris in the Spring", 6)

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:

Dim strMidString As String

strMidString = Mid("Visit Paris today", 7, 5 )