473,705 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trim string.

gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)
Jul 22 '05 #1
8 17728
DrBob wrote:
gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)


Use find_first_not_ of and find_last_not_o f to find the positions of the
first and last non-whitespace characters, then use substr to get only the
part of the string you need.

You should've been able to figure that one out yourself really.

--
Unforgiven

"You can't rightfully be a scientist if you mind people thinking
you're a fool."
Jul 22 '05 #2

"DrBob" <bo*******@yaho o.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method to
trim the trailing spaces... (Assuming a method doesn't exist that I'm
not aware of.)


I am not sure about extending the class, but if the first part of your
string does not contain spaces, you could always just look up the index of
the first space, and cut the end off.

string x("abcd ");
string y(x.begin, x.find(' '));

jonathan
Jul 22 '05 #3
"Jonathan" <no****@aol.com > wrote in message
news:vs******** ****@corp.super news.com
"DrBob" <bo*******@yaho o.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
gcc 3.3 MAC OS X.

I have a string that has trailing spaces in it that I want removed.
So i have a variable:
string x("abcd ");

x.trim() isn't an implemented method.
Is there a method I don't know about?

What do I do to extent the string class such that there is a method
to trim the trailing spaces... (Assuming a method doesn't exist
that I'm not aware of.)


I am not sure about extending the class, but if the first part of your
string does not contain spaces, you could always just look up the
index of the first space, and cut the end off.

string x("abcd ");
string y(x.begin, x.find(' '));

jonathan


And if you want to modify the original string rather than create a new one:

string x("abcd ");
string::size_ty pe st = x.find(' ');
x.erase(st, x.length()-st);
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #4
"John Carson" <do***********@ datafast.net.au > wrote in message
news:3f******** @usenet.per.par adox.net.au
And if you want to modify the original string rather than create a
new one:

string x("abcd ");
string::size_ty pe st = x.find(' ');
x.erase(st, x.length()-st);


Actually, you only need:

string x("abcd ");
x.erase(x.find( ' '));
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #5
In article <3f********@use net.per.paradox .net.au>,
John Carson <do***********@ datafast.net.au > wrote:

string x("abcd ");
x.erase(x.find( ' '));


Of course, that won't work if the string has embedded spaces. How about
searching from the end?

x.erase(x.find_ last_not_of(' ')+1);

Or if we want to trim any whitespace (not just blanks):

x.erase(x.find_ last_not_of(" \t\n")+1);

--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #6
"Jon Bell" <jt*******@pres by.edu> wrote in message
news:bq******** **@jtbell.presb y.edu...
| In article <3f********@use net.per.paradox .net.au>,
| John Carson <do***********@ datafast.net.au > wrote:
| >
| > string x("abcd ");
| > x.erase(x.find( ' '));
|
| Of course, that won't work if the string has embedded spaces. How about
| searching from the end?
|
| x.erase(x.find_ last_not_of(' ')+1);
|
| Or if we want to trim any whitespace (not just blanks):
|
| x.erase(x.find_ last_not_of(" \t\n")+1);

Caveat: npos+1 == ? (the npos return value needs to be tested for).
I suspect both of these will fail if no blank is found ...
Regards,
Ivan
--
http://ivan.vecerina.com

Jul 22 '05 #7
"DrBob" <bo*******@yaho o.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
| I have a string that has trailing spaces in it that I want removed.
| So i have a variable:
| string x("abcd ");
|
| x.trim() isn't an implemented method.
| Is there a method I don't know about?
|
| What do I do to extent the string class such that there is a method to
| trim the trailing spaces... (Assuming a method doesn't exist that I'm
| not aware of.)

No such method => you should implement a non-member function to do so.
I use:
char const kBlankChars[] = " \t\n\r";

/// Returns a string with leading/trailing characters of a set stripped
std::string trimmed
( std::string const& str ///< the original string
, char const* sepSet=kBlankCh ars ///< chars 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);
}

std::string rtrimmed( std::string const& str, char const* sepSet )
{
std::string::si ze_type const last = str.find_last_n ot_of(sepSet);
return ( last==std::stri ng::npos )
? std::string()
: str.substr(0, last+1);
}

std::string ltrimmed( std::string const& str, char const* sepSet )
{
std::string::si ze_type const first = str.find_first_ not_of(sepSet);
return ( first==std::str ing::npos )
? std::string()
: str.substr( first );
}
Let me know if you see any bug in this...
Ivan
--
http://ivan.vecerina.com
Jul 22 '05 #8
"Ivan Vecerina" <pl************ *****@ivan.vece rina.com> wrote in message
news:bq******** **@newshispeed. ch...
| | Or if we want to trim any whitespace (not just blanks):
| |
| | x.erase(x.find_ last_not_of(" \t\n")+1);
|
| Caveat: npos+1 == ? (the npos return value needs to be tested for).
| I suspect both of these will fail if no blank is found ...

Woops... no. I sent this too soon.
In fact:
npos is returned if the string only contains blanks.
npos+1 == 0
So it should be ok.

My apologies,
Ivan
--
http://ivan.vecerina.com

Jul 22 '05 #9

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

Similar topics

22
12751
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous' or even why there could be a problem. here is the code ////////
11
5350
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
7
4223
by: Sascha Herpers | last post by:
Hi, what is the difference between the trim function and the trim String-member? As far as I see it, both return the trimmed string and leave the original string unaltered. Is any of the two faster? Is there a general rule/opinion to prefere members over functions? Thanks for any hint.
13
2638
by: Jonathan Wood | last post by:
According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to modify s. s.Trim('\r'); While the follow code DOES modify s. s = s.Trim(\r');
22
9738
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I thought I'd change it to the VB ..NET method "String.Trim" but that throws an object exception. Which brings the question: is it compliant to use Trim(String), or is it more within etiquette to use If Not String Is Nothing Then String.Trim?
3
3503
by: Pascal | last post by:
bonjour hello I would like to trim a string of all its white spaces so i used myString.trim() but it doesn't work as supposed : unsecable space are remaining in the middle of my string... i read in msdn : and notice that trim only Removes all occurrences of white space characters from the beginning and end of this instance. So what for the middle ? .NET Framework Class Library
31
2903
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char *trim(char *s) { char *begin,*end; begin = s;
4
4095
by: Oleg Subachev | last post by:
If I apply Trim() method to string consisting of two x0D and x0A characters the resulting string is not empty but contain the same two charaters: x0D, x0A. But according to the help Trim() method "removes all occurrences of white space characters from the beginning and end". And CARRIAGE RETURN (U+000D) and LINE FEED (U+000A) are among white space characters removed by the Trim() method.
8
2836
by: Kevin Smith | last post by:
Hi, According to the intellisense help, string.Trim() "Removes all occurances or white space characters from the beginning and end of this instance." However, the follow code does not appear to modify s. s.Trim('\r'); While the follow code DOES modify s.
8
3252
by: Keith Thompson | last post by:
Kevin Smith <no@spam.comwrites: You posted this to microsoft.public.dotnet.languages.csharp, where I presume it's topical. Why on Earth did you redirect followups to comp.lang.c? Anyone else replying to Kevin Smith's article, please *ignore* the Followup-To header and post only to the csharp group. Thanks.
0
8767
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8689
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
9273
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
9030
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
8979
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
7893
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
4439
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4703
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3137
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

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.