Answers for "string::Trim"

0

string::Trim

#include <iostream>
#include <string>
    
using std::string;

// trim from left 
inline std::string& ltrim(std::string& s, const char* t = " tnrfv")
{
	s.erase(0, s.find_first_not_of(t));
	return s;
}
// trim from right 
inline std::string& rtrim(std::string& s, const char* t = " tnrfv")
{
	s.erase(s.find_last_not_of(t) + 1);
	return s;
}
// trim from left & right 
inline std::string& trim(std::string& s, const char* t = " tnrfv")
{
	return ltrim(rtrim(s, t), t);
}

int main()
{
    string str = "             가나다라마바사   ";
	std::cout << str << std::endl;
	trim(str);
	std::cout << str << std::endl;
}
Posted by: Guest on January-23-2022

Browse Popular Code Answers by Language