Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    13

    Unanswered: 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

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

  3. #3
    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"

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    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 on the Tiger 800 or the Norton

  5. #5
    Join Date
    Nov 2003
    Location
    Christchurch, New Zealand
    Posts
    1,618
    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.

  6. #6
    Join Date
    May 2011
    Posts
    13
    Classis VB...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •