Last modified on 8 July 2013, at 21:27

Visual Basic .NET/String and character operators

String OperatorEdit

String concatenationEdit

The "&" operator joins two strings together. Example:

  Dim String1 As String = "123"
  Dim String2 As String = "456"
  Dim String3 As String
  String3 = String1 & String2 ' Results in "123456".

This will result in String3 being equal to "123456"

The "+" operator may be used in place of "&". However, it is not recommended.

ConcatEdit

You may concatenate a number of strings:

  Dim String1 As String = "Let"
  Dim String2 As String = " us"
  Dim String3 As String = " concatenate!"
  Dim strOutput As String
  strOutput = String.concat(String1,String2,String3) ' Results in "Let us concatenate!".

If you have defined a string array, such as:

  Dim strArr As String() = {"I", " am", " your", " automatic", " lover."}

then all the elements of this array may be concatenated in a simple way:

  strOutput = string.concat(strArr) ' Results in "I am your automatic lover."

String manipulations functionsEdit

  • Lcase(): converts in higher case.
  • Ucase(): converts in lower case.

To create some substrings, the VB6 functions are still available:

  • Left(): left part.
  • Right(): right part.
  • Mid(): middle part.
  • InStr(): substring location into a string.
  • Replace(): replace a substring by another one.
    Sub Main()
        Dim StringName As String = "lorem ipsum dolor sit amet"
        StringName = StringName.Replace("i", "o")
        Console.WriteLine(Mid(StringName, InStr(StringName, " "), 6))
        Console.ReadLine()  ' Displays " opsum"
    End Sub