#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    USA
    Posts
    144
    Rep Power
    12

    quick string on multiple lines question


    Hi,

    A quick question about strings - I just want to declare a string varaible over a few lines for readability sake:

    string LongSentence = "this is a long sentence that I want to
    declare over a couple of lines instead
    of on one long long line.";

    I get an error "new line in constant" but I thought the compiler would interpret that as being one string. Of course if I define it all on one line it works fine. Does anyone know how to get the compiler to interpret that as one string??

    Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Contatenating string literals in C is extremely easy. You just place one literal right after the other without any special characters:
    Code:
    string LongSentence = "this is a long sentence that I want to" 
                               "declare over a couple of lines instead "
                               "of on one long long line.";
    Another example of this is when you want to create a string literal that contains compiler constants, such as (using Visual C++ v1.52):
    char g_sVersion[]= { VERSION "," __DATE__ "," __TIME__};
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    You could always use the \ character to indicate to the compiler that the string is continued on the next line.
    Code:
    string LongSentence = "this is a long sentence that I want to \
    declare over a couple of lines instead \
    of on one long long line.";
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    USA
    Posts
    144
    Rep Power
    12

    thanks!


    Thanks guys that was exactly what I was looking for!

IMN logo majestic logo threadwatch logo seochat tools logo