473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

strip on std::string

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

Jul 19 '05 #1
2 24153
Hi Giampero,
"Giampiero Gabbiani" <gi****************@libero.it> wrote in message
news:pa****************************@libero.it...
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
--
http://ivan.vecerina.com
Jul 19 '05 #2
Il Sat, 01 Nov 2003 14:45:14 +0100, Ivan Vecerina ha scritto:
Hi Giampero,
"Giampiero Gabbiani" <gi****************@libero.it> wrote in message
news:pa****************************@libero.it...
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
14
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
5
by: probstm | last post by:
I am using a message handler class that implements amongst others: static void message ( std::string aHeader, std::string aMessage, int aTimeout = 0, MessageType aFlag = MSG_Information ); ...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.