If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > Working with strings

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-11, 10:12
Adam89 Adam89 is offline
Registered User
 
Join Date: May 2011
Posts: 13
Working with strings

Hey up everyone,

I'm just wondering what's the best way to build up a long string in VB? I've tried a few variations but getting errors every time.

For example:

Code:
myVar = "first part of string"
myVar += "second part of string"
Code:
myVar = "first part of string"
myVar &= "second part of string"
Code:
myVar = "first part of string\
second part of string"
As said though I just keep getting errors. It's a long string that will have a lot of variables concatenated within it, so I don't want to be all on the same line for readability purposes.

Thanks for any help,

Adam
Reply With Quote
  #2 (permalink)  
Old 05-17-11, 10:21
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
either all in one
or use the string concatenation symbol '&'
myvar = 'value of my variable: ' & avariable & vbcrlf
myvar = myvar & 'value of my next variable: ' & bvariable
you can use either the ' or " symbols to delimt text
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 05-17-11, 11:00
Adam89 Adam89 is offline
Registered User
 
Join Date: May 2011
Posts: 13
Thanks for your response. I actually discovered the line continuation character ("_") which allows me to achieve roughly what I wanted:

Code:
myVar = "first part of string" _
      & "second part of string"
Reply With Quote
  #4 (permalink)  
Old 05-17-11, 11:22
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
Code:
myVar = "first part of string" _
      & "second part of string"
is syntactically the same as
Code:
myVar = "first part of string" & _
       "second part of string"
and
Code:
myVar = "first part of string" & "second part of string"
it has no direct impact on string concatenation, thats triggered by the & symbol. providing you leave a space before the _ the VB compiler treats subsequent limes as part of the first line. you can use it to merge several lines into one. perhaps its best use is to made the code more legible
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 05-18-11, 00:33
rokslide rokslide is offline
Registered User
 
Join Date: Nov 2003
Location: Christchurch, New Zealand
Posts: 1,617
Are you talking classic Vb (eg VB 6.0) or VB.Net?

If you are talking VB.Net and long strings with lots of additions you might want to look at the Stringbuilder class which is a lot more memory friendly.
Reply With Quote
  #6 (permalink)  
Old 05-18-11, 15:06
Adam89 Adam89 is offline
Registered User
 
Join Date: May 2011
Posts: 13
Classis VB...
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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On