merlinzjl

导航

C++ 常用字符串分割split函数和trim函数

void split(const string &str, vector<string> &res, const char pattern)
{
	istringstream is(str);
	string temp;
	while (getline(is, temp, pattern))
	{
		if (temp.length() != 0)
		{
			res.push_back(temp);
		}
	}
		
	return;
}


void trim(std::string &s) 
{ if (s.empty()) return; s.erase(0, s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); }

  

posted on 2020-09-12 20:35  merlinzjl  阅读(835)  评论(0编辑  收藏  举报