Advertisement

how to parse a string

Started by April 11, 2002 10:20 PM
10 comments, last by GarlandIX 22 years, 7 months ago
What is the best way to parse a string in C++? I think i saw it in a book somewhere, but i can''t remember >_< ... Anyway thanks in advance for your help! ------------------------------ If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?
------------------------------BASIC programmers don't die, they just GOSUB and don't return.
It depends on how your using the string.
If your using it as a C-style string then there are a few standard string functions available in the string.h header.

Not sure exactly how youd like to parse the string but i usually use on of these functions:
strncmp(char *, char *)
strchr(char *, int)
strrchr(char *, int)
or a good function for some text-based games is:
strstr(char *, char *) where it searches the first character string for the first occurance of the second character string.

Just look in your compiler documentation for examples on what they all do.

Not sure if that helps or not.

Conan Bourke.
...I've lost my shirt, I've pawned my rings......I've done all the dumb things...
Advertisement
I am using c-style strings. I would like to find the first occurence of the character ''/'' and extract the characters before and after it. How would i go about this?


------------------------------
If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?
------------------------------BASIC programmers don't die, they just GOSUB and don't return.
This sounds like a custom function to me. I don''t recall any such string function that will fetch the character before and after a character.

Reply back if you need help coming up with the function. It really shouldn''t be that hard.







------

If I''m incorrect, sue me, you''ll get nothing
------Shop for the Lowest Price!Then, Check a Reseller's Rating Before You Purchase!
in that case it wouldn''t be too hard.. simply use the strchr(char *, int) function.

I was just doing something just like this with a chat program i made...

strchr returns a pointer to the location in the string where the int was found (use the int as a char).

an example could be:

char *location, buffer[20], before[20];
int character = ''/'';

strcpy(buffer, "Hi there / goodbye");

location = strchr(buffer, character);

printf("/ is located at %d\n", location-buffer);

then to get everything after / just do

printf("%s", buffer[location-buffer]);

everything before it...

strncpy(before, buffer, location-buffer);

That should work, if not then with a little fiddling it will.

Conan Bourke.
...I've lost my shirt, I've pawned my rings......I've done all the dumb things...
aNonamus is right, it would be easier and more cleaner to write a custom function and use the strchr like i suggested.

Perhaps something like:

char *string, *before, *after;
int character;

void Parse(char *string, int character)

Then just have it assign the global pointers to the right locations. That way the code is at least a little cleaner.

Conan H Bourke.
...I've lost my shirt, I've pawned my rings......I've done all the dumb things...
Advertisement
You also may write something like this:


  char *str1 = "tobe/divided";char * str2;if(strchr(str1, ''/'') != 0){ str2 = strchr(str1, ''/'')+1; str1 = strtok(str1, "/");}  
Mtk: You should copy the strings. If the original string changes, your "tokens" will, too. That may or may not be a problem.

Now, if you''re using C++, please use C++

void parse_string( std::string & str,                          std::vector<std::string> & v,                          const char seps[] = ": \t,.''\"()*&^%$#@!~`-_=+\\|]}[{/?><" ){  int p0, p1;  p0 = str.find_first_not_of( seps, 0 );  if( p0 == std::string::npos )  return;  while( true )  {    p1 = str.find_first_of(seps, p0);    if(p1 == std::string::npos)    {      if( p0 >= static_cast<long>( str.find_last_not_of(seps) ) )        break;      p1 = str.find_last_not_of(seps) + 1;    }    ( p1 <= p0 ) ? ++p1 : v.push_back( str.substr( p0, (p1-p0) ) );    p0 = p1;  }} 


You use the function like so:
#include <iostream>#include <iterator>#include <string>#include <vector>//int main(){  using namespace std;  string statement = "This is how to tokenize a string!";  vector<string> strings;  parse_string( statement, strings );  // use default separators  ostream_iterator<string> osi( cout, ", " );  copy( strings.begin(), strings.end(), osi );  return 0;} 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Conan: your method is closest to what i want to do, but i get an invalid page fault at the line :
printf("%s", buffer[location-buffer]);
If there was a way to extract the information after the / into a vaiable instead, that would be great. Does anyone have any ideas because im stumped.


------------------------------
If someone asks you "A penny for your thoughts" and you put your two cents in, do you get change?
------------------------------BASIC programmers don't die, they just GOSUB and don't return.
quote: Original post by GarlandIX
If there was a way to extract the information after the / into a vaiable instead, that would be great. Does anyone have any ideas because im stumped.

a.) See my post. C++, clean, efficient, and returns an array containing all the tokens (which you can then play with as you see fit).

b.) str[n]cpy.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement