Strings

Started by
8 comments, last by boxdot3 21 years, 10 months ago
I have looked through MSDN to try and find a function equivilent to VBs Mid function, but i wasnt able to find one for c or C++. What i need is something that i can call that will return what is in the string at a specified position. i.e. I have the string "Im a Newb" I want to be able to find the 4th character in the string, which in the case would be "a". Any ideas? Thanx for any help! Simon
Advertisement
C style strings:
char *Str = "Test";char Char4 = Str[3];

STL style strings:
std::string Str = "Test";char Char4 = Str[3];


Wow that was a fast reply! Thanks a lot!

Later

Simon
quote:Original post by Null and Void
char *Str = "Test";

I''m not trying to pick on you or something, but shouldn''t it be

const char *Str = "Test";
---visit #directxdev on afternet <- not just for directx, despite the name
quote:Original post by IndirectX
I''m not trying to pick on you or something, but shouldn''t it be
const char *Str = "Test";

I thought about it, but decided to keep it simple. I had const on there, but I erased it before posting.

If you are after substrings (like VB mid), the last post contains some example C code:

http://www.gamedev.net/community/forums/topic.asp?topic_id=95233

,Jay
me sa newB,
whats the differents between const and no const?

If you don''t change the values in it...

_________________________________________________________________________
Can someone be nice and help me on my way to be the next Hideo Kojima? Thought So...
const will ensure the value won't change. This is especially important with pointers.

Even if you don't change the value the pointer points to, it's still possible that the pointer itself could be mangled accidently.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 8, 2002 11:54:33 AM]

[edited by - Chem0sh on June 8, 2002 11:55:45 AM]
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
quote:Original post by Chem0sh
Even if you don''t change the value the pointer points to, it''s still possible that the pointer itself could be mangled accidently.

There is a difference between pointer to const data and const pointer. For example:

  // pointer to const dataconst char *p = "text";// this won''t compile*p = ''T'';// this is legalp = "another text";// const pointerchar * const p = "text";// this is legal*p = ''T'';// and this won''t compilep = "another text";// const pointer to const dataconst char * const p = "more text";  

I was asking because the string literal might be put into constant memory by the compiler, and an attempt to modify it might cause access violation. I think I read something like "string literals should be const char *, but they are char * so that C code compiles" in Stroustrup but I can''t remember now.
---visit #directxdev on afternet <- not just for directx, despite the name
Bah! std::string is so much simpler.
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/

This topic is closed to new replies.

Advertisement