Library code snippets

String Concatenation

To join two strings together in Visual Basic, you use the & operator. For example,

sString = "hello " & sName

Combines the text "hello " and the value of sName, and saves it in the sString variable. An operator that is notably missing in VB 6 an earlier is a way to append a string to another. In VB 6 or earlier, you have to do

sString = sString & " goodbye!"

Which adds " goodbye" to the end of sString. However, this can be extremely inefficient when performing the operation multiple times, or on large strings. (For performance comparisons with a C++ equivilant, take a look here). Fortunately, with VB.NET, the language has been brought into line with most others, and you can now do

sString .= " goodbye!"

instead.

Comments

  1. 08 Jan 2009 at 18:36
    In Vb.Net, you can use the System.Text.StringBuilder class if you're concatinating over and over again in a loop. It is tons faster. For basic concatenation, the above works well though.
  2. 10 Apr 2002 at 06:48

    say uve got three string in a sentance time|title|location , there broken by the | how would u split the three strings in 3 individual strings again??


    i know how to find info before and after a | but not breaking down three strings
    any help would be appreciated

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of String Concatenation.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback