473,467 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

string rtrim ?

Hi,

This is my rtrim version:
inline void rtrim(string *ptr_s)
{
string::reverse_iterator rit= ptr_s->rbegin();
while(rit != ptr_s->rend() && *rit==' ') ptr_s->erase((++rit).base());
}

Can anyone give any hint to improve it ?

Thanks,
Jose Luis
Jul 22 '05 #1
2 7156
jose luis fernandez diaz wrote:
Hi,

This is my rtrim version:
inline void rtrim(string *ptr_s)
{
string::reverse_iterator rit= ptr_s->rbegin();
while(rit != ptr_s->rend() && *rit==' ') ptr_s->erase((++rit).base());
}

Can anyone give any hint to improve it ?

Thanks,
Jose Luis

Here's a few ideas:
1. pass a reference in stead of a pointer - you don't have to orry about
NULL in that case
2. make it a template function - that way, you're not restricted to just
strings but can use the same function on things that have a similar
interface
3. find the last non-space with find_last_not_of and call erase only
once with that information
4. search for more than just spaces - i.e. any white space (horizontal &
tabs, nelines, carriage returns, etc.) or (if you go with point 2.)
supply a second tempalte argument with a list of whitespaces that
defaults to the one you want to use by default
<untested_code>
template <typename C, const C whitespace = " \t\n\r">
inline void rtrim(std::basic_string<C> & s);
</untested_code>
HTH

rlc
Jul 22 '05 #2
jo**********************@yahoo.es (jose luis fernandez diaz) wrote in
news:c2**************************@posting.google.c om:
Hi,

This is my rtrim version:
inline void rtrim(string *ptr_s)
{
string::reverse_iterator rit= ptr_s->rbegin();
while(rit != ptr_s->rend() && *rit==' ')
ptr_s->erase((++rit).base());
}

Can anyone give any hint to improve it ?

Thanks,
Jose Luis


void rtrim(string & str)
{
str.erase(str.find_last_not_of(" "), str.end());
}
?
Jul 22 '05 #3

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

Similar topics

2
by: David Lawson | last post by:
I need to trim off trailing 0x00's from a binary string. Does anyone know how? David
5
by: Aamer Nazir | last post by:
Hi, I am having problems setting the value of a variable in a SQL String that I have to create dynamically in my procedure. The code that I currently have is as follows: set...
4
by: rajdb2 | last post by:
Hi, I am using the following sql statement SELECT rtrim(rtrim(coalesce(substr(char(v.creationdate),1,4) || '-' || substr(char(v.creationdate),6,2) || '-' || substr(char(v.creationdate),9,2) ||...
9
by: alan | last post by:
Hello, can somebody help me with this self written rtrim function? i do always get a access violation on the line where i want to terminate the string: str = '\0'; why can't i acces this specific...
10
by: satishrajana | last post by:
Hi, My SQL returns a NULL in a datefield if there is no date in that field. If there is a NULL in this column, I want to replace it with spaces in my SELECT statement when I am selecting these...
27
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
1
code green
by: code green | last post by:
I have an address field in a table that contains one, two, three or more lines of an address. I need to split this into three fields for later insertion into CSV. I have done something similar...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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 ...

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.