Results 1 to 4 of 4

Thread: string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    string

    hi!
    I am coding in c#

    I have to put "abcd" in a string variable. Had it been abcd only I would have written

    Code:
    string str = "abcd";
    str then wud have contained abcd.

    but the problem is str has to contain "abcd".

    Hope, I am clear in my post.
    Thanks.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: string

    like this ...
    VB Code:
    1. [COLOR=Blue]string[/COLOR] str = @"""abcd""";
    2.     Console.WriteLine(str);
    3.            
    4. [COLOR=Green]    // outputs "abcd"  ( including the " )
    5.     // if you want the . at the end ... string str = @"""abcd""."; [/COLOR]
    hope it helps
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: string

    In C# you can escape any single character with a '\' character so you could use

    string str = "\"abcd\"";

    Preceeding a string with an '@' character tells the compiler to take the entire string as literal. That means that @"\n" is not a new-line character but actually a string that contains the characters '\' and 'n'. In the case of double quotes, as dynamic_sysop shows, you still have to escape them with another double quote to show that they are not a string delimiter.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Malaysia
    Posts
    345

    Re: string

    Thanks a bunch.
    Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width