how to split the string into 2 strings

Started by
23 comments, last by Drew_Benton 19 years, 2 months ago
ok i have a configuration file format of my own and i read teh file puting it into a list. then from the i want to take each line of the list and split the string into two strings. so here is a example of a string i want tpo split: "graphic_api = d3d;" now what i want to do is split it so that from the beginning of the string to the "=" char is a list, exclude the space char: "graphic_api" not "graphic_api " and then split from the "=" character to the ";" character like this: "d3d" not " d3d: just so i hopew you know what i mean here would be another example: oringal strng: "keyboard_active = 1;" the new strings: "keyboard" "1" please can someone help me.
Advertisement
Google for Lex(A Lexical Analyzer Generator) and Yacc(Yet Another Compiler-Compiler).
that is not what i am looking for, i have me own in-game database format i am using and just need to find out how to split a string like that stated before, thank for that info tho.
hmm, like a tokenizer? You could do something like this(assuming you're using C++, since you didn't say what language you're using):
std::vector<std::string> tokenize(std::string string) {    std::string temp;    std::vector<std::string> strings;    for (int i = 0; i < string.length(); i++) {        if (string == " " || string == "\t") {            strings.push_back(temp);            continue;        } else if (string == ";") {            strings.push_back(temp);            break;        }        temp += string;    }    return strings;}
Also, if your just using this for key/value pairs, it's bit overkill and you might want to consider XML.
People will hate me, but for small jobs like this I'd just bite the bullet and use strtok. It has one job in the world and this is it, and you don't have to write lots of XML or C++ stream code that does the same thing. You could also manually parse it yourself character by character, but meh. MEH I say! [grin]
well there are 2 programmers on thius project, me and an programmer, and we want this to be more of a learning experiance if nothing else so when we talked about XML he knew alot but i did not know much about this stuff so he and i though it might be nice for me to create our own file format thing.
well, all XML really is are key/value pairs(at least, that's what you'd use it for), like this:
<key>value</key>
or, you could do:
<object field1="value1" field2="value2" />
This sort of thing is why regular expressions were invented. ^\s*(\w+)\s*=\s*(\w+);\s*$ or whatever variations your chosen implementation supports.
strtok is actually a bit trickier to use if you want to validate the data's format and not just blindly grab tokens, so you'd probably be better off with a manual parser that goes character by character. It's a better learning experience and you can do the format validation too.
Quick and easy solution based on that input you gave[smile]:
#include <string>#include <iostream>using namespace std;int main(int argc, char* argv[]){	// Original string	string str = "graphic_api = d3d;";	// String one	string str1 = str.substr( 0, str.find("=") );	// Removes the spaces from the end	str1 = str1.substr(0, str1.find(" ") - 1);	// String two	string str2 = str.substr( str.find("=") + 1, str.find(";") - str.find("=") - 1 );	// Removes the spaces from the beginning	str2 = str2.substr(str2.find(" ") + 1,str.size());	cout << str1;	cout << str2;		return 0;}


Can be optimized of course, but that's just my quick little example. You would of course have to change it up a bit if you had something like
"graphic_api  =  d3d;"
Etc..., but that shouldn't be to hard.

- Drew

[edit] I would recommend just looping through once and stripping out all of the spaces to make your life easier, then the code would become this:
int main(int argc, char* argv[]){	// Original string, after you strip out ALL spaces programatically	string str = "graphic_api=d3d;";	// String one	string str1 = str.substr( 0, str.find("=") );	// String two	string str2 = str.substr( str.find("=") + 1, str.find(";") - str.find("=") - 1 );	cout << str1;	cout << str2;		return 0;}


That is what I did with my parser stuff.

This topic is closed to new replies.

Advertisement