Changes

Jump to: navigation, search

Working with Strings in Visual Basic

1,721 bytes added, 19:31, 1 August 2007
New page: 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 progr...
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.

== 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:

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

strSalutation = strTitle & " " & strFirstName & " " & strLastName
</pre>

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:

<tt>Mr. Fred Smith</tt>

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"

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

strSalutation = "Hello" & strTitle & " " & strFirstName & " " & strLastName
</pre>

== Finding the Length of a String ==

Navigation menu