## Random code

posted in Lame? Where?!
Published December 13, 2005
Advertisement
I was recently talking with a good friend of mine about how she likes PHP over C++ because of it's ease of usability. In this case, string manipulation. C++ doesn't have a split functio nto split strings by a deliminator, which PHP does, so it's handy. In discussing with her various ways of accomplishing such a task, I decided to write a small function to do it.

Now I know there are those of you out there who will look at this code and see fifty better ways of doing it, or some will look at it and say "There's already a way of doing that," or will look at it and say "That's not very optimized." Or any other plethora of things. But, I don't care. It wasn't written to be optimized, I didn't write it the best way possible, or anything like that, simply because I'm not the best. I'm a mediocre programmer who was just doing this as a test for myself to see if I could successfully write a split function for strings. So without further ado, here it is.

EDIT: I just noticed there was some old code that doesn't need to be there. It's better now.

#include #include #include using namespace std;vector split(char sep, const string &str);void main() {	string rooms = "Room 1;Room 1 Description;Exits;Parameter 4;Fuck off!";	vector roominfo;	roominfo = split(';', rooms);	for (int i = 0; i < (int)roominfo.size(); i++)		cout << roominfo << endl;}/////////////////////////////////////////////////vector split(char sep, const string &str){	// Define "no more seperators found"	static const basic_string <char>::size_type npos = -1;	string strCopy = str;		// Mak a copy so we can screw with it and not harm the original	vector tmpString;	size_t position;		// Get the first position of the seperator	position = strCopy.find_first_of(sep);		// If position is ever <= zero, that means that there is no seperator in the string	while (position != npos) {		// Add the first slice to the array		tmpString.push_back(strCopy.substr(0, position));		// We are erasing up to the position of the separator, plus one, to erase the		//	seperator as well.		strCopy.erase(0, position + 1);				// Get the next position of the seperator		position = strCopy.find_first_of(sep);	}		// When the position is zero, that means that we have no more seperators.  All that's	//	left is the last one, and goes into the last part of the array.	tmpString.push_back(strCopy);	return tmpString;}


I know, not the most glamorous, but it works wonderfully.

The outcome on her progress getting it done is, she got her own code working without having to look at mine. Her project is well off on it's way and I'm quite proud of her for what she's been able to accomplish thus far. She's learning quite well. :-)
Previous Entry ## Argh
Next Entry ## Peanuts
0 likes 1 comments

Comments

mattd
void Tokenize (const std::string& in, std::vector<std::string>& out, const std::string& delim)
{
    out.clear();

    size_t tokenPos = in.find_first_not_of(delim);
    size_t delimPos = in.find_first_of(delim, tokenPos);
    while(tokenPos != std::string::npos)
    {
        out.push_back(in.substr(tokenPos, delimPos - tokenPos));
        tokenPos = in.find_first_not_of(delim, delimPos);
        delimPos = in.find_first_of(delim, tokenPos);
    }
}



:P
December 14, 2005 04:38 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Math is hard

1242 views

## uhh

1441 views

## lol

1190 views

## new gaem?!

1130 views

## GH Tourney

1257 views

## Guitar Hero

1203 views

## Beyond Hell

1177 views
Advertisement