Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim / remove a tab ( "\t" ) from a string

Tags:

c++

string

tabs

Can anyone suggest a way of stripping tab characters ( "\t"s ) from a string? CString or std::string.

So that "1E10      " for example becomes "1E10".

like image 861
AndyUK Avatar asked Feb 17 '09 10:02

AndyUK


People also ask

Does string trim remove tab?

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

How do I remove a tab from a string?

replace('\t', '') . The replace method will remove the tabs from the string by replacing them with empty strings.


1 Answers

hackingwords' answer gets you halfway there. But std::remove() from <algorithm> doesn't actually make the string any shorter -- it just returns an iterator saying "the new sequence would end here." You need to call my_string().erase() to do that:

#include <string>
#include <algorithm>    // For std::remove()

my_str.erase(std::remove(my_str.begin(), my_str.end(), '\t'), my_str.end());
like image 179
j_random_hacker Avatar answered Sep 23 '22 19:09

j_random_hacker