Strings in Visual Basic


© Swapna Kamat
Articles in this Topic    Discussions in this Topic

String Manipulation

Strings play a very important part in development of a program in any programming language. Text output given to the user, error messages, etc are all displayed using strings. So it is imperative to know the string handling techniques very well. This article just serves as an introduction to string concept. More on strings will be dealt with later.

String Operations

Programming languages like C/C++ do not have string as a built-in data type. String operations are carried out considering them to be arrays of characters. Use of library files like string.h and strng.h empower the user to carry out various string operations like comparison, concatenation, difference, character count, string length, etc.

Visual Basic supports only one string operator, the concatenation operator. This operator combines two or more strings of text, similar to the way the addition operator is used to combine two or more numbers. The concatenation operator is the ampersand symbol (&). When you combine two strings with the concatenation operator, the second string is added directly to the end of the first string. The result is a longer string containing the full contents of both source strings.

The syntax of the concatenation operator is

NewString = stringOne & stringTwo & stringThree

In this syntax, NewString represents the variable that contains the result of the concatenation operation. stringOne, stringTwo, and stringThree all represent string expressions. These can be any valid strings, including string variables, literal expressions (enclosed in quotation marks), or functions that return a string.

The ampersand between a pair of string expressions tells Visual Basic to concatenate the two expressions. The ampersand must be preceded and followed by a space. The syntax shows an optional second ampersand and a third string expression. You can combine any number of strings with a single statement. Just remember to separate each pair of expressions with an ampersand.

This is in the case that stringOne, stringTwo and stringThree are defined as string variable using

Dim stringOne As String

If we directly want to add a phrase to a string variable, it can be achieved as follows

Msg = "Visual Basic is Fun."
Msg = Msg & "and it is very powerful."

This can be achieved in one statement using the space_ operator

Msg  = "Visual Basic is Fun." & _
            "and it is very powerful."

Next time, we shall see the control structure statements in Vb6.

Go To Page: 1


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo