strip on std::string

  • Thread starter Giampiero Gabbiani
  • Start date
G

Giampiero Gabbiani

Hi to all,
Is there a simple way to implement a strip algorithm on std::string using
STL?
I'm sure that it's possible to implement it using some transform, but my
knowledge of STL is POOR.

Regards
Giampiero
 
I

Ivan Vecerina

Hi Giampero,
Giampiero Gabbiani said:
Is there a simple way to implement a strip algorithm on std::string using
STL?
I'm sure that it's possible to implement it using some transform, but my
knowledge of STL is POOR.

I am not sure what a 'strip algorithm' is...
To remove characters (e.g. blanks) from the beginning and the end of the
string (what I call 'trimming'), the following function can do:

/// Returns a string with leading/trailing characters of a set stripped
std::string trimmed
( std::string const& str ///< the original string
, char const* sepSet ///< C string with characters to be dropped
)
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}

Sample usage:
string name = trimmed( userInput, " \t" ); // trip spaces and tabs


Not sure if this is what you were looking for...


Ivan
 
G

Giampiero Gabbiani

Il Sat, 01 Nov 2003 14:45:14 +0100, Ivan Vecerina ha scritto:
Hi Giampero,


I am not sure what a 'strip algorithm' is...
To remove characters (e.g. blanks) from the beginning and the end of the
string (what I call 'trimming'), the following function can do:

/// Returns a string with leading/trailing characters of a set stripped
std::string trimmed
( std::string const& str ///< the original string
, char const* sepSet ///< C string with characters to be dropped
)
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}

Sample usage:
string name = trimmed( userInput, " \t" ); // trip spaces and tabs


Not sure if this is what you were looking for...


Ivan
Thank you very much Ivan,
it's just what I was looking for... (confused with similar function
in REXX...)
Again, thx.
Giampiero
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Chatbot 0
std::string on "const char *" 16
c-style string vs std::string 20
Crossword 2
std::map<std::string,int> Problem 3
Advancing Through std::vector 15
TF-IDF 2
std::for_each on a map containing unique_ptr 6

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top