Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2004, 09:59 AM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default String vs. StringBuilder

Which method is more efficient when adding repeated values to create one long string? Is it better to just concatenate on a string object, str1 = "Hello" & "There" & "Bob" or would using the StringBuilders append option be more effecient in code -
strBuilder.append("Hello")
strBuilder.append("There")
strBuilder.append("Bob")

Right now I'm trying to performance tune an application and it seems that these types of statements are taking up quite a bit of time. I'm pulling values from a database using a ODBC data reader and trying to create one long string record.

The way it's set up now I'm using a For-Next loop to iterate through all the fields returned by the datareader and then use a string builder to append the information. A sample of the code is below.

For intFieldCounter = 0 to (drPrev.FieldCount-1)
     strRecord.append(drPrev(intfieldcounter))
Next
 
Old June 29th, 2004, 10:55 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Use the StringBuilder. It is optimized for building strings. Concatenation is slow as you have found.

A very useful method on the StringBuilder is the AppendFormat() method. String also has something similar (String.Format()). You can use this to append strings that are made of hard coded strings and variable values:

strName = "Peter"
strBuilder.AppendFormat("Hello, my name is {0}", strName)
 
Old June 30th, 2004, 07:31 AM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, that's what I figured but it doens't hurt to check. One thing you mentioned in your reply; I've seen it in other examples and books as well is the use of {}. What does this do?

strBuilder.AppendFormat("Hello, my name is {0}", strName) Does the 0 correspond to anything in particular? I've wondered about it but never thought to ask what it does.

 
Old June 30th, 2004, 08:05 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The {n} refers to a format replacement token:
Code:
String.Format("{2} {1} {0}." , "Peter", "my name is", "Hello")
                ^   ^   ^      |_____|  |__________|  |_____|
                |   |   |_________/          |           |
                |   |________________________/           |
                |________________________________________/


The format methods (String.Format, StringBuilder.AppendFormat) take 1, 2 or 3 "Object" arguments, or an array of objects. You can use more than three replacement tokens with the overload that accepts an array. The tokens can be anywhere in the format string, and can occur multiple times. The token doesn't have to be in the string, but if there is a token in the string, there must be an accompanying object (either in the 1-3 argument list, or in the array).

Peter
-------------------------
Work smarter, not harder





Similar Threads
Thread Thread Starter Forum Replies Last Post
StringBuilder welshboy2005 C# 5 October 20th, 2010 06:19 AM
String And StringBuilder - A Common Mistake Muhammad Zeeshan C# 1 November 23rd, 2007 04:12 AM
StringBuilder select statement GS C# 2005 4 August 26th, 2007 01:42 PM
XmlDocument Vs StringBuilder anup_daware C# 4 February 8th, 2007 09:54 AM
Examples For String And StringBuilder jayakumar.cj ASP.NET 1.0 and 1.1 Basics 0 July 15th, 2006 07:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.