Answer by Jonah for How to trim a std::string?
While most of the above solution would work well, but I would like to propose a solution I use for specific use case where space are placed repetitively in a chaos order at either left, right or both...
View ArticleAnswer by ricab for How to trim a std::string?
Trim left and right:trimmed_str = std::regex_replace(original_str, std::regex{R"(^\s+|\s+$)"}, "");Trim right:trimmed_str = std::regex_replace(original_str, std::regex{R"(\s+$)"}, "");Trim...
View ArticleAnswer by Soup Endless for How to trim a std::string?
using std::find_if_not and reverse iterator (no +1/-1 adjustments) and returning number of spaces trimmed// returns number of spaces removedstd::size_t RoundTrim(std::string& s){ auto const...
View ArticleAnswer by Soumadip Dey for How to trim a std::string?
you can you use this function to trim you string in c++void trim(string& str){ while(str[0] == '') str.erase(str.begin()); while(str[str.size() - 1] == '') str.pop_back();}
View ArticleAnswer by Matthias for How to trim a std::string?
Poor man's string trim (spaces only):std::string trimSpaces(const std::string& str){ int start, len; for (start = 0; start < str.size() && str[start] == ''; start++); for (len =...
View ArticleAnswer by Arty for How to trim a std::string?
str.erase(0, str.find_first_not_of("\t\n\v\f\r ")); // left trimstr.erase(str.find_last_not_of("\t\n\v\f\r ") + 1); // right trimTry it online!
View ArticleAnswer by ragnarius for How to trim a std::string?
Trims both ends.string trim(const std::string &str){ string result = ""; size_t endIndex = str.size(); while (endIndex > 0 && isblank(str[endIndex-1])) endIndex -= 1; for (size_t i=0;...
View ArticleAnswer by antb52 for How to trim a std::string?
why not use lambda?auto no_space = [](char ch) -> bool { return !std::isspace<char>(ch, std::locale::classic());};auto ltrim = [](std::string& s) -> std::string& {...
View ArticleAnswer by Anil Gupta for How to trim a std::string?
I have read most of the answers but did not found anyone making use of istringstream std::string text = "Let me split this into words";std::istringstream iss(text);std::vector<std::string>...
View ArticleAnswer by BullyWiiPlaza for How to trim a std::string?
The accepted answer and even Boost's version did not work for me, so I wrote the following version:std::string trim(const std::string& input) { std::stringstream string_stream; for (const auto...
View ArticleAnswer by Sadidul Islam for How to trim a std::string?
Here is a solution for trim with regex#include <string>#include <regex>string trim(string str){ return regex_replace(str, regex("(^[ ]+)|([ ]+$)"),"");}
View ArticleAnswer by Jackt for How to trim a std::string?
Ok this maight not be the fastest but it's... simple.str = " aaa ";int len = str.length();// rtrimwhile(str[len-1] == '') { str.erase(--len,1); }// ltrimwhile(str[0] == '') { str.erase(0,1); }
View ArticleAnswer by Phidelux for How to trim a std::string?
With C++17 you can use basic_string_view::remove_prefix and basic_string_view::remove_suffix:std::string_view trim(std::string_view s){ s.remove_prefix(std::min(s.find_first_not_of(" \t\r\v\n"),...
View ArticleAnswer by UnSat for How to trim a std::string?
Below is one pass(may be two pass) solution. It goes over the white spaces part of string twice and non-whitespace part once.void trim(std::string& s) { if (s.empty()) return; int l = 0, r =...
View ArticleAnswer by user3229557 for How to trim a std::string?
I know this is a very old question, but I have added a few lines of code to yours and it trims whitespace from both ends.void trim(std::string &line){ auto val = line.find_last_not_of(" \n\r\t") +...
View ArticleAnswer by cute_ptr for How to trim a std::string?
Here's a solution easy to understand for beginners not used to write std:: everywhere and not yet familiar with const-correctness, iterators, STL algorithms, etc...#include <string>#include...
View ArticleAnswer by nulleight for How to trim a std::string?
Here is my version:size_t beg = s.find_first_not_of(" \r\n");return (beg == string::npos) ? "" : in.substr(beg, s.find_last_not_of(" \r\n") - beg);
View ArticleAnswer by Kemin Zhou for How to trim a std::string?
Here is a straight forward implementation. For such a simple operation, you probably should not be using any special constructs. The build-in isspace() function takes care of various forms of white...
View ArticleAnswer by elxala for How to trim a std::string?
As I wanted to update my old C++ trim function with a C++ 11 approach I have tested a lot of the posted answers to the question. My conclusion is that I keep my old C++ solution! It is the fastest one...
View ArticleAnswer by user1438233 for How to trim a std::string?
c++11:int i{};string s = " h e ll \t\n o";string trim = " \n\t";while ((i = s.find_first_of(trim)) != -1) s.erase(i,1);cout << s;output:helloworks fine also with empty strings
View Article
More Pages to Explore .....