Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

## Random code

Sign in to follow this  


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.

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.

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. :-)
Sign in to follow this  

1 Comment

Recommended Comments

void Tokenize (const std::string& in, std::vector<std::string>& out, const std::string& delim)


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);


Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!