|
-
Nov 23rd, 2001, 12:44 PM
#1
Thread Starter
Addicted Member
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.
-
Nov 23rd, 2001, 02:24 PM
#2
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|