Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

string find question

lets say the strings are of this format

<any chars here><static string here><any chars here>

e.g..
thisIsATeststatic735645646blahdyblah
45454346static735645646fgfdgfdgfdg
dgfdgfdgfgfsdgdfdstatic735645646dgfdgfdg

as you can see the static string is static735645646

how can i grab the bit *after* this static string?
so..

thisIsATeststatic735645646blahdyblah should give me blahdyblah
2nd one should give me fgfdgfdgfdg
3rd one should give me dgfdgfdg

hope you guys can help me out
[576 byte] By [pouncer] at [2007-11-11 7:49:45]
# 1 Re: string find question
I would use find and substr from the string class..

//Find
string str1 = "thisIsATeststatic735645646blahdyblah";
string str2 = "static735645646";
size_type position = str1.find (str2,0);
cout << position << endl;

int length = (strlen(str1) - position);
//Substr

string str3 = str1.substr(position, length);
cout<<str3; //blahdyblah

I havent tested the code, but it should work :)
Code_Nerd at 2007-11-11 21:01:56 >
# 2 Re: string find question
perfect, thank you very much.
pouncer at 2007-11-11 21:02:56 >
# 3 Re: string find question
No probs.. :)
Code_Nerd at 2007-11-11 21:03:56 >
# 4 Re: string find question
actually its giving me erros now on

size_type position = str1.find (str2,0);

saying undeclared identifier size_type :S
pouncer at 2007-11-11 21:04:50 >
# 5 Re: string find question
you need to #include <string> and qualify size_type with std::. Since size_type is just an alias for size_t, you can use size_t instead (not recommended, but it works on all compilers I can think of).
Danny at 2007-11-11 21:05:55 >
# 6 Re: string find question
you need to #include <string> and qualify size_type with std::. Since size_type is just an alias for size_t, you can use size_t instead (not recommended, but it works on all compilers I can think of).

Yes sorry didnt add that..

Thanks Danny :)
Code_Nerd at 2007-11-11 21:06:54 >
# 7 Re: string find question
You're welcome!
btw, what time is it in Melbourne right now? It must be late night. Here (Israel) it's almost 9:00 am.
Danny at 2007-11-11 21:07:58 >
# 8 Re: string find question
No its 6:30pm on Thursday.. :)
Code_Nerd at 2007-11-11 21:08:56 >
# 9 Re: string find question
oh, 6:30 pm is the perfecr time for C++;) I'm really more productive in the evening.
Danny at 2007-11-11 21:09:54 >