Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 29th, 2004, 09:59 AM
Authorized User
 
Join Date: Mar 2004
Location: Maumee, OH, USA.
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
Reply With Quote
  #2 (permalink)  
Old June 29th, 2004, 10:55 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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)
Reply With Quote
  #3 (permalink)  
Old June 30th, 2004, 07:31 AM
Authorized User
 
Join Date: Mar 2004
Location: Maumee, OH, USA.
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.

Reply With Quote
  #4 (permalink)  
Old June 30th, 2004, 08:05 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:43 PM.


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