suibenzhi
4/15/2018 - 6:22 AM

c++去掉开头的某些字符

c++去掉开头的某些字符

#include <iostream>
#include <vector>


//分割字符串
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
	std::string::size_type pos1, pos2;
	pos2 = s.find(c);
	pos1 = 0;
	while(std::string::npos != pos2)
	{
		v.push_back(s.substr(pos1, pos2-pos1));

		pos1 = pos2 + c.size();
		pos2 = s.find(c, pos1);
	}
	if(pos1 != s.length())
		v.push_back(s.substr(pos1));
}



/**
* 去掉开头的某些字符,字符之间用","隔开
* s: 需要处理的字符串
* c: 要删掉的字符列表,字符之间用","隔开
* 
*/
void RemoveCharfromBegin(string &s,string c)
{
	if(s.empty() || c.empty())
	{
		return;
	}
	vector<string> v;
	SplitString(c,v,",");
	
	string::iterator it;
	vector<string>::iterator itv;
	
	it = s.begin();
	itv = v.begin();
	while (it != s.end()) {
		int size = 0;
		itv = v.begin();
		while(itv != v.end())
		{
			if(*it == (*itv)[0])
            {
                size++;
			}

			itv++;
		}
		if(size == 0)
		{
			break;
		}

		it++;
	}

	s.erase(s.begin(),it);

}


int main(int argc, char *argv[]) 
{		
	string stTest = "  \n hello world   \n    ";
	
	RemoveCharfromBegin(stTest," ,\n");
	
	cout << stTest << endl;
	
}