Concatenating

There are a couple of ways you can concatenate strings.


System.String.ConCat

string sName; 
sName = System.String.Concat("firstname", "lastname");


+ and +=

string sName; 
sName = "firstname" + "lastname"
sName = sName + "somethingelse"
sName += "somethingelse
Dim sName As String 
sName = "firstname" & "lastname"

This can be used to ammend a string to itself plus something else

sName = sName & "something else" 
sName &= "something else"

System.String.Join

Concatenates all the elements of a string array using a given separator

String.Join(seperator as string, 
      value() as string) as string
String.Join(seperator as string,
       value() as string, _
      startindex as Integer, _
     count as Integer) as string

StringBuilder

Everytime you concatenate a string a new address in memory is allocated
The existing string is copied to a new location with the newly string added
When you use StringBuilder the same position in memory is used

StringBuilder oStringBuilder = new StringBuilder(); 
foreach (var value in mystring)
{
   oStringBuilder.Append(value)
}

Important

In VB.Net you can also use the plus (+) sign as a concatenation operator but this should be avoided as it is also used for adding numeric values.


© 2020 Better Solutions Limited. All Rights Reserved. © 2020 Better Solutions Limited TopPrevNext