std::string

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
User avatar
Red Squirrel
Posts: 29209
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

std::string

Post by Red Squirrel »

For those that program C++, google search std::string's, you will love yourself for learning these! SO MUCH easier to do string handling and stuff! I'm currently writing a parser for news emails and it's crazy how much easier these special type of strings are making it! You don't have to worry about how big they are (no need to allowcate space), you can perform lot of useful operations like substr, find, replace, etc... while these are not always useful alone, they are useful for making parsing functions.

ex:

Code: Select all

string RSP_findbetweenI(string before,string after,string main)
{
string mainI = RSP_tolower(main);

unsigned long int position1 = mainI.find(RSP_tolower(before));
if (string::npos==position1)return "";

unsigned long int position2 = mainI.find(RSP_tolower(after),(position1+before.length()));
if (string::npos==position2)return "";

if(position1>=position2)return "";

position1 = position1 + before.length();
position2 = position2 - position1;

return main.substr(position1,position2);
}
The name of this function is self explanetory to what it does. Think of how this can be useful when parsing data... now imaging writing this function with normal strings. :o First off, the function would have to be limited to a certain allowcated amount of space since you'd have to do something like char before[1024], and the function call would eat up memory since even if you dont use it, you still need to allowcate all that memory.

And without the find function, chars are very hard to manupulate.

Archived topic from Anythingforums, old topic ID:2073, old post ID:30710
Honk if you love Jesus, text if you want to meet Him!
Locked