Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: String Concatenation

From Wiki

Jump to: navigation, search

Summary: An example of how strings can be concatenated with the StringBuilder class

If you concatenate a string like this:

  1. Dim s As String = ""
  2. s &= "This "  
  3. s &= "is "  
  4. s &= "an "  
  5. s &= "example "  
  6. Label1.Text = s

then you actually end up with 5 strings in memory rather than just the original one that you created. This obviously isn't very efficient.

Instead, you can use the System.Text.StringBuilder class which is both quicker and uses less memory as only one object is created:

  1. Dim sb As New StringBuilder
  2. sb.Append("This ")  
  3. sb.Append("is ")  
  4. sb.Append("an ")  
  5. sb.Append("example")  
  6. Label1.Text = sb.ToString()


This Hack is part of the ASP.NET Hacks collection

467 Rating: 5.0/5 (1 vote cast)