Click to See Complete Forum and Search --> : Custom String Class


LittleGreenGecko
09-15-2001, 03:39 PM
I am attempting to create a custom string class in order to parse a file a little easier.

Does anyone have an idea as to if there is already a string class, and how to use it.

I found a C++ template book but it did not have that object defined.

Please help, I am trying to parse a file, and what is the easiest way? :eek:

debiandude
09-15-2001, 03:42 PM
Can you give an example of the type of strings you are trying to parse and what you want to do with them?

LittleGreenGecko
09-15-2001, 03:56 PM
I am attempting to parse source code for multiple languages. These strings will include the following: A-Z, a-z, 0-9, ~`@#$%^&*()-_=+ and others. These will be with or without spaces. What I was thinking was to use this

streamvar.get(char *buffer);

and send the character if it is within a certain ASCII range into the current string, or start a new string. What kind of string to hold those characters is what I am trying to find out.

debiandude
09-15-2001, 04:13 PM
So you have this string - *string and you want to put characters that fall into a certain range into a current string, or if that fall out of that range put them into a new string. Couldn't you do something like this then , and have the add and new functions add the char to the string? Or am I just missing the point.

while(*string) {

if(*string < 100 && *string > 10) add_char(*string++);
else create_new(*string++);
}


[ 15 September 2001: Message edited by: debiandude ]

Strike
09-15-2001, 04:46 PM
There are STL strings and STL ropes, the former is better for smaller implementations and the latter is better for large long strings.

To use them:

#include <string>
using namespace std;

Then string foo; and you have a string :)

Docs on it: http://www.sgi.com/tech/stl/

pinoy
09-15-2001, 05:02 PM
I don't exactly understand what you're trying to do, but it sounds like, the string stream (<sstream> ) and string (<string> ) might be useful to you.

ie. you can do something like:

//...
string str = "foo bar blah";
ostringstream ost;

for (string::iterator s = str.begin(); s != str.end(); str++) {
if (char_in_range(*s))
str << *s;
}

string result = ost.str();
cout << result << endl;

LittleGreenGecko
09-15-2001, 05:30 PM
Is there a document that describes the methods and properties of string.h?

pinoy
09-15-2001, 06:41 PM
You don't want <string.h>, You want <string>. There is a difference. <string.h> or <cstring> is a C header (it declares strcmp, strlen, etc).

Go to the STL documentation at SGI's site (Strike's already provided the link). Look for basic_string.

sans-hubris
09-15-2001, 10:19 PM
Originally posted by LittleGreenGecko:
<STRONG>I am attempting to parse source code for multiple languages. These strings will include the following: A-Z, a-z, 0-9, ~`@#$%^&*()-_=+ and others. These will be with or without spaces. What I was thinking was to use this

streamvar.get(char *buffer);

and send the character if it is within a certain ASCII range into the current string, or start a new string. What kind of string to hold those characters is what I am trying to find out.</STRONG>
So you're going to have certain characters as sentinal characters? Can you test for just the sentinal? If you're just testing for those charactors, that should make things easier. C++ has an STL string class and a related stringstream class that can do this.