Sie sind auf Seite 1von 4

String Manipulation

String Manipulation Using + and & signs.


Strings can be manipulated using the & sign and the + sign, both perform the string concatenation which means
combining two or more smaller strings into larger strings. For example, we can join "Visual" and "Basic" into
"Visual Basic" using "Visual"&"Basic" or "Visual "+"Basic", as shown in the example below
Example 1
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim text1, text2, text3 As String
text1 = "Visual"
text2 = "Basic"
text3 = text1 + text2
Label1.Text = text3
End Sub
End Class

The line text3=text1+ text2 can be replaced by text3=text1 & text2 and produced the same output.
However, if one of the variables is declared as numeric data type, you cannot use the + sign, you can
only use the & sign.
Example 2
Dim text1, text3 as string
Dim Text2 As Integer
text1 = "Visual"
text2=22
text3=text1+text2
Label1.Text = text3

This code will produce an error because of data mismatch.However, using & instead of + will be all right.
Dim text1, text3 as string
Dim Text2 As Integer
text1 = "Visual"
text2=22
text3=text1 & text2
Label1.Text = text3

You can combine more than two strings to form a larger strings, like the following example:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim text1, text2, text3, text4, text5, text6 As String
text1 = "Welcome"
text2 = " to"
text3 = " Visual"
text4 = " Basic"
text5 = " 2010"
text6 = text1 + text2 + text3+text4+text5
Label1.Text = text6
End Sub
End Class

String Functions
String
method

Description

InStr

Returns an integer specifying the start position of the first occurrence of


one string within another.

InStrRev

Returns the position of the first occurrence of one string within another,
starting from the right side of the string.

Join

Returns a string created by joining a number of substrings contained in an


array.

LCase

Returns a string or character converted to lowercase.

Left

Returns a string containing a specified number of characters from the left


side of a string.

Len

Returns an integer that contains the number of characters in a string.

LSet

Returns a left-aligned string containing the specified string adjusted to the


specified length.

LTrim

Returns a string containing a copy of a specified string with no leading


spaces.

Mid

Returns a string containing a specified number of characters from a string.

Replace

Returns a string in which a specified substring has been replaced with


another substring a specified number of times.

Right

Returns a string containing a specified number of characters from the right


side of a string.

RSet

Returns a right-aligned string containing the specified string adjusted to the


specified length.

RTrim

Returns a string containing a copy of a specified string with no trailing


spaces.

Space

Returns a string consisting of the specified number of spaces.

Split

Returns a zero-based, one-dimensional array containing a specified number


of substrings.

StrComp

Returns -1, 0, or 1, based on the result of a string comparison.

StrConv

Returns a string converted as specified.

StrDup

Returns a string or object consisting of the specified character repeated the


specified number of times.

StrReverse

Returns a string in which the character order of a specified string is


reversed.

Trim

Returns a string containing a copy of a specified string with no leading or


trailing spaces.

UCase

Returns a string or character containing the specified string converted to


uppercase.

Example:
Dim strMainString As String = " Hello Visual Basic "
Dim intMainInteger As Integer = 2010
Me.txtResult.AppendText(strMainString & intMainInteger & vbCrLf)
Me.txtResult.AppendText(strMainString + Str(intMainInteger) + vbCrLf)
Me.txtResult.AppendText(UCase(strMainString) & vbCrLf)
Me.txtResult.AppendText(LCase(strMainString) & vbCrLf)
Me.txtResult.AppendText(LTrim(strMainString) & vbCrLf)
Me.txtResult.AppendText(RTrim(strMainString) & vbCrLf)
Me.txtResult.AppendText(Trim(strMainString) & vbCrLf)
Me.txtResult.AppendText(Len(strMainString) & vbCrLf)
Me.txtResult.AppendText(Mid(strMainString, 6, 10) & vbCrLf)
Me.txtResult.AppendText(Replace(strMainString, "i", "1") & vbCrLf)
Me.txtResult.AppendText(InStr(3, strMainString, "i") & vbCrLf)
Me.txtResult.AppendText(StrComp(strMainString, "Hello") & vbCrLf)
Me.txtResult.AppendText(StrComp(strMainString, " Hello Visual Basic ") & vbCrLf)
Me.txtResult.AppendText(StrComp(strMainString, " Hello Visual Basic Hello Visual Basic ") &
vbCrLf)

Das könnte Ihnen auch gefallen