Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

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: 2.8/5 (6 votes cast)