473,883 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 24215
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::si ze_type const first = str.find_first_ not_of(sepSet);
return ( first==std::str ing::npos )
? std::string()
: str.substr(firs t, str.find_last_n ot_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::si ze_type const first = str.find_first_ not_of(sepSet);
return ( first==std::str ing::npos )
? std::string()
: str.substr(firs t, str.find_last_n ot_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
8207
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3681
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( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
5
8768
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) throw(std::runtime_error)
14
2701
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 pthread_mutex_lock and pthread_mutex_unlock from different std::string methods. Since the strings I'm using are never used in another thread I was wondering whether I could get rid of these mutex calls altogether? Are there some compiler options available...
22
13350
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; char c; std::string str;
84
15957
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
2599
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 ); I then use a set of defines, e.g.:
11
2912
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 "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
13
3681
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 <string>
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10863
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6007
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.