zoukankan      html  css  js  c++  java
  • 【C++】去除字符串string中的空格(两头空格、所有空格)

    去除首尾空格:

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

    去除所有空格:

    void trim(string &s)
    {
    	int index = 0;
    	if(!s.empty())
    	{
    		while( (index = s.find(' ',index)) != string::npos)
    		{
    			s.erase(index,1);
    		}
    	}
    }
    
  • 相关阅读:
    例2-3
    例2-2
    例2-1
    p14
    第一次作业
    例1-1
    第二次作业(2)
    第二次作业
    第三章3-3
    第三章3-2
  • 原文地址:https://www.cnblogs.com/dindin1995/p/13059075.html
Copyright © 2011-2022 走看看