string parsing (c++)
Hello there,
Im currently trying work out how to parse data from a winsock connection. Id like to be able to know weather or not there is a function similar to the MID() in VB. I need to read in a certain peice of the string at a certain length into the string. Ive tried strcmp() and a few others with little luck.
If someone could point me into the right direction that would be great.
Thanks
character arrays, or c++ string class?
This might help you.
http://www.robtougher.com/servlet/web.writing.show_article?article=strings
This might help you.
http://www.robtougher.com/servlet/web.writing.show_article?article=strings
For string handling as with the MID function, that is you wish to extract a substring from a larger string, you can just use the offsets of the array with something like strcpy. If you just wish to compare them, then use strncmp.
For example, to see if the string "AAABBBCCCDDD" has "BBB" as the 4th through 6th characters, something like this:
char test_string[] = "AAABBBCCCDDD";
bool find_bbb( const char* to_find )
{
// this is roughly equivalent to MID(to_find$,4,6) = "BBB" in VB
return !strncmp( &to_find[4], "BBB", 3 );
}
void func( void )
{
if( find_bbb( test_string )) {
cout << "Found BBB!" << endl;
}
Hope that helps.
For example, to see if the string "AAABBBCCCDDD" has "BBB" as the 4th through 6th characters, something like this:
char test_string[] = "AAABBBCCCDDD";
bool find_bbb( const char* to_find )
{
// this is roughly equivalent to MID(to_find$,4,6) = "BBB" in VB
return !strncmp( &to_find[4], "BBB", 3 );
}
void func( void )
{
if( find_bbb( test_string )) {
cout << "Found BBB!" << endl;
}
Hope that helps.
Thats a great example, thank you!.
But i am also wondering about something....
The only problem that i see with using the std::string is that the type that is coming off the socket is a character array. Is there any way to convert a character array to a std::string?
I guess you can just recast it (std::string)chararray yes ?
But i am also wondering about something....
The only problem that i see with using the std::string is that the type that is coming off the socket is a character array. Is there any way to convert a character array to a std::string?
I guess you can just recast it (std::string)chararray yes ?
You don't even have to cast, std::string has copy ctor and assignment operator that takes a const char*.
This sort of illustrates some of the examples
The output should be
abc
def
def
#include <iostream>#include <string>using namespace std;int main(){ string s = "abc"; char* c = "def"; string* t; t = new string(c); string u = c; cout << s << "\n" << *t << "\n" << u << "\n"; return 0;}
The output should be
abc
def
def
Quote:Original post by jsloan
I guess you can just recast it (std::string)chararray yes ?
woa woa! preferably not. The implicit conversion constructor MIGHT kick in and help you out, but if not; well hope you can swim.
Just
std::string myString( myCharBuff );
and you're done. Now you can compare myString to either other std::strings or char*. Plus you can do alot more.
[Edited by - Seriema on September 27, 2004 7:47:25 AM]
Thanks for the helpful tip!
Is there any way to create an array of strings?
I need to break up a string and put the parts into an array.
for instance you have
doesnt seem possible though ;|
Is there any way to create an array of strings?
I need to break up a string and put the parts into an array.
for instance you have
std::string str1 = "This is a test";//i want to break it up//so i can have something likestd::string str2;str2[0] = "This";str2[1] = "is";str2[2] = "a";str2[3] = "test";
doesnt seem possible though ;|
Quote:Original post by jsloan
Thanks for the helpful tip!
Is there any way to create an array of strings?
yes two ways (really three but prefer these two):
#include <string>#include <algorithm>#include <iterator>#include <vector>#include <iostream>int main() { //static array & is statically allocated, array cannot grow std::string static_foo[] = { "hello", "goodby", "how are your" }; //dynamic array & is dynamically allocated internally, can grow in size: std::vector<std::string> dyn_foo; dyn_foo.push_back("hi"); dyn_foo.push_back("jim"); std::copy(static_foo, static_foo + 3, std::ostream_iterator<std::string>(std::cout, ", ")); std::copy(dyn_foo.begin(), dyn_foo.end(), std::ostream_iterator<std::string>(std::cout, ", ")); return 0;}
Quote:Original post by jsloan
I need to break up a string and put the parts into an array.
for instance you have
*** Source Snippet Removed ***
doesnt seem possible though ;|
look at std::stringstreams to parse to and from strings in memory to any type desired, here is a silly example:
#include <sstream>#include <string>#include <iostream>int main() { std::string word; int value(0); std::stringstream ss("Hi 43 foo bar 39"); ss >> word; std::cout << word << '\n'; ss >> value; std::cout << value << '\n'; ss >> word; std::cout << word << '\n'; ss >> word; std::cout << word << '\n'; ss >> value; std::cout << value << '\n'; return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement