473,811 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ 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 7180
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_o f 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_cod e>
template <typename C, const C whitespace = " \t\n\r">
inline void rtrim(std::basi c_string<C> & s);
</untested_code>
HTH

rlc
Jul 22 '05 #2
jo************* *********@yahoo .es (jose luis fernandez diaz) wrote in
news:c2******** *************** ***@posting.goo gle.com:
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.f ind_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
2346
by: David Lawson | last post by:
I need to trim off trailing 0x00's from a binary string. Does anyone know how? David
5
13377
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 @sqlStatement='Set @compare_string=' + '(Select ' + @group_column_list_mod + ' from ' + @Tbl_Name + '_Sorted' + ' where Identity_Column=' + ltrim(rtrim(str(@loop_counter))) + ')'
4
4469
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) || ' ' || substr(char(v.creationdate),12,2) || ':' || substr(char(v.creationdate),15,2) || ':' || substr(char(v.creationdate),18,2), '')) ||'~' || rtrim(coalesce(substr(char(v.lastmodifieddate),1,4) || '-' ||
9
2669
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 position? void rtrim(char *str) { for( int i=strlen(str)-1; i>= 0; i-- ) {
10
39165
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 records. Can any of you experts out there help me with a sample SQL.
27
6885
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 4+) : String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')") String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')") String.prototype.Trim= new...
1
17471
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 with the name field which I don't lile because I am testing the same condition twiceCASE WHEN CHARINDEX(' ',RTRIM(LTRIM(full_name))) > 0 THEN RTRIM(LTRIM(SUBSTRING(full_name,CHARINDEX(' ',full_name)+1,20))) ELSE RTRIM(LTRIM(full_name)) END...
0
10651
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...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
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,...
0
10136
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
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
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
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.