PDA

Click to See Complete Forum and Search --> : string to char


DarkMoose
Nov 23rd, 2001, 11:44 AM
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?

parksie
Nov 23rd, 2001, 01:24 PM
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:char myChar = string("penguin")[2]; // This is the 3rd character - zero based