String question

Started by
9 comments, last by v0dKA 17 years, 9 months ago
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.
Advertisement
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]
.:<<-v0d[KA]->>:.
Awesome, thanks. Sorry about the stupid question, I thought that only applied to char arrays.
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
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.
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?
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.
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...
.:<<-v0d[KA]->>:.
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...
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

This topic is closed to new replies.

Advertisement