Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ String , plx help

Started by anguspang Apr 11, 2009 at 4:11 PM 9 replies 2.4k views
Original Post
anguspang
anguspang
i have some string like this: my name is i use the getline, it will separate into 3 parts my, name, is i want to igonore the and , so it can separate to my, name, is how can i separate ? (ignore the and )
Will-O
Will-O
Not sure whether you're using char * or std::string but...

if you're using char * this is how you might go about it if say you were gettinga string from a text doc or sumin like that:
char *wholeLine = new char[100];  // make the buffer bigger if 100 is too smallstd::ifstream f_in;               // file-stream objectf_in.open("source.txt", std::ios::in | std::ios::beg); // use stream to openf_in.get(wholeLine, 99, '\n');    // get to end of line or 99 chars// Now for your string-searching...char *pStart = strchr(wholeLine, '>');  // pointer to the beginning++pStart;                               // move the pointer to after charchar *pEnd = strchr(pStart, '<');       // pointer to endsize_t strLen;if(pStart != NULL && pEnd != NULL)  strLen = pEnd - pStart;          // notice way roundelse  strLen = 0;char *usefulChars = new char[strLen];  // space for the stuff you wantmemcpy(usefulChars, pStart, strLen);   // copy it into the new spaceusefulChars[strLen] = '\0';// do what ever you want with "usefulChars" here ...// now kill off the strings...memset(wholeLine, '\0', 100);wholeLine = NULL;memset(usefulChars, '\0', strLen);usefulChars = NULL;



... that's one way. I have written that directly into the webpage so please check it in your IDE but I think you'll get the jist of it from what I've put. Hope it helps.

nullsquared
nullsquared
You can certainly manually parse it as shown above. However, that looks like XML... are you trying to parse XML? If so, why not use a simple library that already does it for you, such as tinyXML?
theOcelot
theOcelot
Quote:
Original post by nullsquared
You can certainly manually parse it as shown above. However, that looks like XML... are you trying to parse XML? If so, why not use a simple library that already does it for you, such as tinyXML?


Or this one. It's pretty sweet. Just one .h and .cpp, you can just add a copy of it to your project. It appears to support Unicode, though I haven't tested that. It's biggest drawback in my opinion is that it doesn't support DTD's but that's probably not relevant in this situation anyway. I'm using it in my game to parse files containing animation sequences. It depends on absolutely nothing but the C standard library, so it's cross-platform.
anguspang
anguspang
if AD<
how can improve in this case?
SriLumpa
SriLumpa
If it's not XML you are parsing, you can try extracting the useful part with some regular expression. Boost has some.
Will-O
Will-O
In the absence of other libraries and so on it is possible to extract the string you require. But not sure whether it's the '<' or the "
char *pEnd = strchr(pStart, '<');  // find the charsize_t usefulLen = 0;for(size_t i = 0; i < 100; ++i) // I assume the line's less than 100 chars {  usefulLen = pStart - pEnd;       // num useful chars  size_t temp = usefulLen + 1;     // increment by one  pEnd = strchr(pStart+temp, '<'); // search from next unknown byte  i = (pEnd != NULL) ? i: 101;     // if pEnd is now a null poitner, get out}// and then the same char *usefulChars = new char[usefulLen] etc...


The snippets in http://www.cplusplus.com/reference/clibrary/cstring/ are great for learning this stuff.
nullsquared
nullsquared
Quote:
Original post by theOcelot
Quote:
Original post by nullsquared
You can certainly manually parse it as shown above. However, that looks like XML... are you trying to parse XML? If so, why not use a simple library that already does it for you, such as tinyXML?


Or this one. It's pretty sweet. Just one .h and .cpp, you can just add a copy of it to your project. It appears to support Unicode, though I haven't tested that. It's biggest drawback in my opinion is that it doesn't support DTD's but that's probably not relevant in this situation anyway. I'm using it in my game to parse files containing animation sequences. It depends on absolutely nothing but the C standard library, so it's cross-platform.


Generally looks OK, except for:
Quote:

For example: it does NOT use slow STL::String class but plain, simple and fast C malloc 's.

/facepalm
theOcelot
theOcelot
Quote:
Original post by nullsquared
Quote:
Original post by theOcelot
Quote:
Original post by nullsquared
You can certainly manually parse it as shown above. However, that looks like XML... are you trying to parse XML? If so, why not use a simple library that already does it for you, such as tinyXML?


Or this one. [snip]


Generally looks OK, except for:
Quote:

For example: it does NOT use slow STL::String class but plain, simple and fast C malloc 's.

/facepalm


Yeah, it's not really designed as a modern C++ library. I've been thinking it might be kind of fun to renovate it for modern C++. Replace malloc/free with new/delete, use templates instead of the preprocessor for character-independence. Maybe someday when I'm more experienced and don't have anything better to do. I would also have to write some better documentation. In the meantime, though, it's hard to beat the simplicity.

Besides the needless swipe at std::string, what exactly is so evil about malloc? I'm not trying to ask a rhetorical question: I think I've heard good reasons to avoid malloc. Just curious.
Zakwayda
Zakwayda
Quote:
Besides the needless swipe at std::string, what exactly is so evil about malloc? I'm not trying to ask a rhetorical question: I think I've heard good reasons to avoid malloc. Just curious.
There's nothing inherently evil about malloc (not that I know of at least). However, its usefulness in C++ is limited, and if you don't know exactly what you're doing, using it in C++ can cause problems.

Rather than try and explain further, I'll just direct you here (I just skimmed over the article, but it looks like it offers a pretty good summary of the issue).
Antheus
Antheus
Quote:
Original post by theOcelot

Besides the needless swipe at std::string, what exactly is so evil about malloc? I'm not trying to ask a rhetorical question: I think I've heard good reasons to avoid malloc. Just curious.


Write an algorithm which parses simple XML document (stored in char *), and displays alphabetically sorted unique tags. You are not allowed to use any kind of dynamic allocation at all (new/malloc/....).

The basis of such algorithm would be an array holding char *s to tags inside original char * pointing to XML text. strcmp and other routines operating on this array would be modified to respect how a tag is represented. For example, a string pointed to by such char * would terminate at space, zero character or >.

Above is not only doable, but quite simple once you grasp why dynamic allocation is often redundant. It does however simplify many algorithms, and gives one considerably larger working space. Above algorithm will be limited by stack space, although it can be made to operate on arbitrary memory buffer, and even on top of std::vector, without losing any of it's no-allocation characteristics.

While very rarely used today, such techniques can bring often surprising run-time improvements. Once you compare to number of new calls performed by STL, it's easy to understand why some consider it slow.

Managed languages can often outperform naive C++ algorithms when it comes to strings as well, since garbage collection plays well with heavy string processing and frequent arbitrary-sized allocations. It's part of why toString() has become so overused in such languages.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.