Wrox Programmer Forums
| 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
  #1 (permalink)  
Old November 10th, 2004, 03:25 PM
Registered User
 
Join Date: Nov 2004
Location: , KS, .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Doldrums Send a message via AIM to Doldrums Send a message via MSN to Doldrums Send a message via Yahoo to Doldrums
Default Multiline Strings

What is the most optimized way to write a very long string literal? Take the following for an example.

If I have:

Dim S As String
S = "This is a long line of text that I would really like to break up on multiple lines"


Would it be best to do this:

S = "This is a long line of text " + _
    "that I would really like to " + _
    "break up on multiple lines"


Or this:

S = "This is a long line of text "
S += "that I would really like to "
S += "break up on multiple lines"


Or this:

S = "This is a long line of text "
S &= "that I would really like to "
S &= "break up on multiple lines"


Or, do all of these examples create 3 strings and destroy 2?

Thanks,

Doldrums
  #2 (permalink)  
Old November 10th, 2004, 06:36 PM
Friend of Wrox
 
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default

All of the methods you mentioned will work, however they are not optimised for performance.

This doesn't matter if you're just creating one string. However, for a large number of string operations (say string concatenation in a large loop), you'd be better off using one of the following methods:

Method 1
--------
Code:
Dim s As String
s = String.Format ("{0}{1}{2}", _
        "This is a long line of text ", _
        "that I would really like to ", _
        "break up on multiple lines")
Method 2
--------
Code:
Dim s As String
Dim sb As New System.Text.StringBuilder

sb.Append("This is a long line of text ")
sb.Append("that I would really like to ")
sb.Append("break up on multiple lines")
s = sb.ToString
However, as I said, the performance hit from doing a concatenation such as
Code:
S = "This is a long line of text " & _
    "that I would really like to " & _
    "break up on multiple lines"
is pretty minimal. As I said, just don't put it in a big loop.



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiline lable kripa ostwal Visual Basic 2005 Basics 1 April 6th, 2007 06:41 AM
AcceptsReturn multiline textbox suzila General .NET 19 March 10th, 2006 02:28 AM
Storing Multiline Textbox value. rupen Classic ASP Basics 7 July 20th, 2005 02:21 PM
Regular Expression for Multiline TextBox Dilip Nagle General .NET 3 January 6th, 2005 02:22 AM





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