Archived

This topic is now archived and is closed to further replies.

Alpha_ProgDes

accessing strings

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites