Sign in to follow this  
divineleft

String question

Recommended Posts

Part of my game engine relies on modifying strings based on the users input. I was wondering if there is any way to extract a character from a string with a number as a parameter which tells how many places over to take it from. For example, say we have the string: "bookmarks" Say I needed to get the character that is 6 places over from the right (a), how would I go about doing that? I'm not sure if there is a function that does just that, but I honestly can't figure it out.

Share this post


Link to post
Share on other sites
If you're using character arrays, you use [] just as in a normal array. If you're using std::string, it defines the [] operator and you use it exactly the same way as character arrays. [ EDIT: The only difference is that you can't assign values with the [] operator on strings. Apparently, you can. Never knew that :) ]

EDIT: or you can use the std::string::at() function. The at() function is actually safer than the [] operator, because it won't let you reference items outside the bounds of the string. If you try to, it will throw an exception.

[Edited by - v0dKA on July 22, 2006 5:40:50 PM]

Share this post


Link to post
Share on other sites
Quote:

The only difference is that you can't assign values with the [] operator on strings.

Say what? - I probably misunderstood you, but the following will work:

std::string pData("abc");
pData[0] = 'z'; //zbc

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
Quote:

The only difference is that you can't assign values with the [] operator on strings.

Say what? - I probably misunderstood you, but the following will work:

std::string pData("abc");
pData[0] = 'z'; //zbc


Indeed. This is well-defined and standard. You wouldn't, of course, be able to do it to a const std::string, but then you couldn't to a const char* either.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I apologize in advance if this is a really stupid question, but, if std::strings are so awesome, what the heck are char arrays for anyways?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I apologize in advance if this is a really stupid question, but, if std::strings are so awesome, what the heck are char arrays for anyways?


C-compatability.

Also, you can't use STL types across DLLs easily, so char arrays have to do.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I apologize in advance if this is a really stupid question, but, if std::strings are so awesome, what the heck are char arrays for anyways?


This may or may not be a good idea, but char arrays are also used as byte arrays. A char data type is almost always exactly 1 byte long. So when you need an array of bytes, you simply use an array of chars.

It's bad to make assumptions, however. [ EDIT: There's nothing that guarantees that a char must be exactly one byte long. Oh yea there is, like, the C++ Standard itself :) ] I'm frankly not sure what you would use instead...

Share this post


Link to post
Share on other sites
If you need an array of bytes, then create a byte array. No need to mess around with an array of chars instead. That's just confusing and you're bound to misuse it somewhere.

EDIT: My mistake. It seems that C++ doesn't have a "byte" type. I've been messing around with Java for too long...

Share this post


Link to post
Share on other sites
Quote:
Original post by v0dKA
It's bad to make assumptions, however. There's nothing that guarantees that a char must be exactly one byte long. I'm frankly not sure what you would use instead...


I'm pretty sure that char is the only integral type of guarenteed size.

I believe the standard dicates that sizeof(char) == 1

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by v0dKA
It's bad to make assumptions, however. There's nothing that guarantees that a char must be exactly one byte long. I'm frankly not sure what you would use instead...


I'm pretty sure that char is the only integral type of guarenteed size.

I believe the standard dicates that sizeof(char) == 1


Oh. Duly noted.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this