* @brief Trims the given string. * * @param[in] str String to be trimmed. * @param[in] toTrim String of characters to be trimmed (removed) from the * beginning and the end of @a str. By default, it contains * all whitespace characters from the ASCII set. * * @return Trimmed string. * * For example, trim(" hey there ", " ") returns "hey * there"</t
| 564 | * there"</tt>. |
| 565 | */ |
| 566 | std::string trim(std::string str, const std::string &toTrim) { |
| 567 | // Based on |
| 568 | // http://www.codeproject.com/Articles/10880/A-trim-implementation-for-std-string |
| 569 | std::string::size_type pos = str.find_last_not_of(toTrim); |
| 570 | if (pos != std::string::npos) { |
| 571 | str.erase(pos + 1); |
| 572 | pos = str.find_first_not_of(toTrim); |
| 573 | if (pos != std::string::npos) { |
| 574 | str.erase(0, pos); |
| 575 | } |
| 576 | } else { |
| 577 | str.erase(str.begin(), str.end()); |
| 578 | } |
| 579 | return str; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * @brief Splits the given string by a separator. |