Hanya sharing pengalaman: nge blog, google AdSense, cyber enjoyable

Naruto Fans Kampanye Damai Pemilu Indonesia 2009 Mendukung SBY untuk menjadi Presiden 2009-2014

Jul 6, 2007

Concatenating Strings

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 concatenation operator is used in an assignment statement as follows:

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.

Code example:

Private Sub Command1_Click()

Dim STR1, STR2 As String

STR1 = "mas Basy"

STR2 = " Belum Makan"

Form1.Print (STR1 & STR2)

End Sub

Reversing the Order of Characters in Strings

Visual Basic's new StrReverse() function enables you to completely reverse the order of a string. Its syntax is as follows:

strResult = StrReverse(strMyString)

In this syntax,

  • Text2 is the string returned from the function.
  • Text1 is the function name.

Visual Basic defaults all internal variables to valid values--numeric variables default to zero, and strings default to empty. The problem happens when you take a null data field from a database and assign it to an internal variable or when you perform a string function on the null data field.

In this version, a loop was required to step through each character in the string and then build the new string in reverse order. The code in Listing below delivers the same result with the new "StrReverse()" function, with no loops and only one simple command.

Code example:

Private Sub Form_Load()

Text1.Text = "Pitik Jago Kluruk 34X"

Text2.Text = ""

End Sub

Private Sub Command1_Click()

Text2.Text = StrReverse(Text1.Text)

End Sub


Alexa Rank