根据token分割字串

#include <iostream>
#include <string>
#include <cstring>

int main()
{
	const char *c = "2:212,2:213,2:214,2:215,2:216,2:217,2:218,2:219,2:220,2:221,2:222,2:223,2:224,2:225,2:226,2:227,2:228,2:229,2:230,2:231,2:232,2:233,2:234,2:235,2:236,2:237,2:238,2:239,2:240,2:241,2:242,2:243,2:244,2:245,2:246,2:247,2:248,2:249,2:250,2:251,2:252,2:253,2:254,2:255,2:256,2:257,2:258,2:259,2:260,2:261,2:262,2:263,2:264,2:265,2:266,2:267,2:268,2:269,2:270,2:271,2:272,2:273,2:274,2:275,2:276,2:277,2:278,2:279,2:280,2:281,2:282,2:283,2:284,2:285,2:286,2:287,2:288,2:289,2:290,2:291,2:292,2:293,2:294,2:295,2:296,2:297,2:298,2:299,2:300,2:301,2:302,2:303,2:304,2:305,2:306,2:307,2:308,2:309,2:310,2:311,";
	//const char *c = "2:212";
	int beginPos = 0;
	int endPos = 0;
	std::string buf = c;
	const char *token = ",";
	while (true) {
		beginPos = buf.find_first_not_of(token, beginPos);
		if (beginPos == (int)std::string::npos) {
			break;
		}
		endPos = buf.find_first_of(token, beginPos);
		if ((int)std::string::npos == endPos) {
			break;
		}

		std::string sub = buf.substr(beginPos, endPos - beginPos);
		std::cout << "sub = " << sub << std::endl;
		beginPos = endPos;
	}

	return 0;
}

 

posted @ 2017-04-06 14:34  Kjing  阅读(476)  评论(0编辑  收藏  举报