Skip to main content
gamedev.net
🔒 Locked

accessing strings

Started by Alpha_ProgDes Nov 7, 2002 at 11:27 AM 3 replies 500+ views
Original Post
Alpha_ProgDes
Alpha_ProgDes
is there a way to access a string like you access an char array? ex: char *name = "John Smith"; string name1 = "John Smith"; letter = name[3]; //letter would equal 'n'; // how would you do this for string name? edit: string name is changed to string name1 for clarity. [edited by - Alpha_ProgDes on November 7, 2002 12:46:39 PM]
Beginner in Game Development?  Read here. And read here.  
Hanz
Hanz
I would do it like this:
quote:
Original post by Alpha_ProgDes
std::string name = "John Smith";

char letter = name[3]; //letter would equal 'n';




[edited by - Hanz on November 7, 2002 12:32:27 PM]
jediknight219
jediknight219
I believe that the [] operator is overloaded so that you can access your string as name1[index]. There''s also name1.at(index1). And if you''re really desperate, you can get a c-style string from name1.cstr().
Aaargh! Everyone save your workspaces before your program locks up your computer!
Oluseyi
Oluseyi
quote:
Original post by Alpha_ProgDes
anything?

Read more carefully. Hanz altered your original post to change name to std::string, indicating that operator[] was already overloaded for it to behave just like it would with a null-terminated char array.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.