Answer by Toby Speight for How can I trim a std::string?
The code presented doesn't take account of all the whitespace characters the active locale defines. It also only works with strings of char, not handling wider types.It's more efficient to accept and...
View ArticleHow can I trim a std::string?
I'm currently using the following code to right-trim all the std::strings in my programs:std::string s;s.erase(s.find_last_not_of(" \n\r\t") + 1);It works fine, but I wonder if there are some...
View ArticleAnswer by Greg Hewgill for How can I trim a std::string?
In the case of an empty string, your code assumes that adding 1 to string::npos gives 0. string::npos is of type string::size_type, which is unsigned. Thus, you are relying on the overflow behaviour of...
View ArticleAnswer by Leon Timmermans for How can I trim a std::string?
Using Boost's string algorithms would be easiest:#include <boost/algorithm/string.hpp>std::string str("Hello, World! ");boost::trim_right(str);str is now "Hello, World!". There's also trim_left...
View ArticleAnswer by Steve for How can I trim a std::string?
I'm not sure if your environment is the same, but in mine, the empty string case will cause the program to abort. I would either wrap that erase call with an if(!s.empty()) or use Boost as already...
View ArticleAnswer by Paul Nathan for How can I trim a std::string?
Hacked off of Cplusplus.comstd::string choppa(const std::string &t, const std::string &ws){ std::string str = t; size_t found; found = str.find_last_not_of(ws); if (found != std::string::npos)...
View ArticleAnswer by Evan Teran for How can I trim a std::string?
For C++11#include <algorithm> #include <cctype>#include <locale>// Trim from the start (in place)inline void ltrim(std::string &s) { s.erase(s.begin(), std::find_if(s.begin(),...
View ArticleAnswer by Bill the Lizard for How can I trim a std::string?
Use the following code to right trim (trailing) spaces and tab characters from std::strings (ideone):// trim trailing spacessize_t endpos = str.find_last_not_of(" \t");size_t startpos =...
View ArticleAnswer by Corwin Joy for How can I trim a std::string?
The above methods are great, but sometimes you want to use a combination of functions for what your routine considers to be whitespace. In this case, using functors to combine operations can get messy,...
View ArticleAnswer by tzaman for How can I trim a std::string?
Here's what I came up with:std::stringstream trimmer;trimmer << str;trimmer >> str;Stream extraction eliminates whitespace automatically, so this works like a charm. It is pretty clean and...
View ArticleAnswer by Michaël Schoonbrood for How can I trim a std::string?
I like tzaman's solution. The only problem with it is that it doesn't trim a string containing only spaces.To correct that one flaw, add a str.clear() in between the two trimmer lines:std::stringstream...
View ArticleAnswer by Brian for How can I trim a std::string?
This version trims internal whitespace and non-alphanumerics:static inline std::string &trimAll(std::string &s){ if(s.size() == 0) { return s; } int val = 0; for (int cur = 0; cur <...
View ArticleAnswer by user818330 for How can I trim a std::string?
Try this. It works for me.inline std::string trim(std::string& str){ str.erase(str.find_last_not_of('') + 1); // Suffixing spaces str.erase(0, str.find_first_not_of('')); // Prefixing spaces return...
View ArticleAnswer by Brian W. for How can I trim a std::string?
Here is yet another option. It removes one or more characters from both ends.string strip(const string& s, const string& chars=" ") { size_t begin = 0; size_t end = s.size()-1; for(; begin <...
View ArticleAnswer by DavidRR for How can I trim a std::string?
My solution based on the answer by @Bill the Lizard.Note that these functions will return the empty string if the input string contains nothing but whitespace.const std::string StringUtils::WHITESPACE...
View ArticleAnswer by David G for How can I trim a std::string?
Now C++11 is here, we have lambdas and auto variables. So my version, which also handles all-whitespace and empty strings, is:#include <cctype>#include <string>#include...
View ArticleAnswer by Some programmer dude for How can I trim a std::string?
With C++11 also came a regular expression module, which of course can be used to trim leading or trailing spaces.Maybe something like this:std::string ltrim(const std::string& s){ static const...
View ArticleAnswer by Duncan for How can I trim a std::string?
What about this...?#include <iostream>#include <string>#include <regex>std::string ltrim(std::string str) { return std::regex_replace(str, std::regex("^\\s+"),...
View ArticleAnswer by synaptik for How can I trim a std::string?
This is what I use. Just keep removing space from the front, and then, if there's anything left, do the same from the back.void trim(string& s) { while(s.compare(0,1,"")==0) s.erase(s.begin()); //...
View ArticleAnswer by Jorma Rebane for How can I trim a std::string?
I guess if you start asking for the "best way" to trim a string, I'd say a good implementation would be one that:Doesn't allocate temporary stringsHas overloads for in-place trim and copy trimCan be...
View Article