Results 1 to 2 of 2

Thread: string to char

  1. #1

    Thread Starter
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185

    string to char

    char myChar;
    string s = "penguin";

    myChar = s.substr(3, 1);

    The idea is for myChar to have the value of 'g' after this, but I get an error because s.substr(3, 1); is still considered a string. How can I convert that letter into a character?
    To understand recursion, one must first understand the concept of recursion.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Re: string to char

    Originally posted by DarkMoose
    char myChar;
    string s = "penguin";

    myChar = s.substr(3, 1);
    If you only want a single character, just use the overloaded [] operator:
    Code:
    char myChar = string("penguin")[2]; // This is the 3rd character - zero based
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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