473,404 Members | 2,137 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,404 software developers and data experts.

Removing the leading and tailing blanks

I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA

Oct 13 '05 #1
6 6072
> trimIt( std::string s ) {

First, try this...

trimIt( std::string& s) {

Oct 13 '05 #2
ben
jalkadir wrote:
I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA


Use your debugger to step through the code. Take the temporaries out so
you can examine their values.

Ben
Oct 13 '05 #3
jalkadir wrote:
I am trying to find a way to remove the leading and tailing blanks from
a string.
What I have come up with is not doing the work as I expected; because
if the value I pass to the function is " string " the leading and
trailing blanks are not removed. What am I doing wrong?

trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_not_of( "\t\n" ) + offset );
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

TIA


A couple of things

1) There is no space in "\t\n", you mean " \t\n".

2) The call to trim trailing spaces is simply incorrect, it does nothing
of the sort. It's more likely to crash your program than anything else.

john
Oct 13 '05 #4
Thanks John!
I did as you suggested, but it causes a segmentation fault.
I get a message saying: 'This application has requested the Runntime to
terminate it in an unusual way.'
I am using GCC-g++-3.4.2 on a MSWin-XP.

I would like to know if you have a tangible example showing how to
solve this problem; if so, could you share it?

TIA

Oct 13 '05 #5
std::string trimIt( std::string s ) {
size_t offset = s.length();
// Trim leading spaces ???
s.erase( 0, s.find_first_not_of( " \t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_last_not_of( " \t\n" ) + 1);
// This line is always displayed
if(s.length() == offset){ std::cout << "No Changes" << std::endl;}
return s;
}

Does this work for you?
Note: in the original post you wasn't passing the string by reference
so i left it unchanged.

Oct 13 '05 #6
Yes!!
Thanks Paulius-maruska, it did the trick.

Oct 15 '05 #7

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

Similar topics

16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
1
by: Jean-michel | last post by:
I need to convert a decimal field to char() but also trim the leading zeroes. Any idea? I could not find any function to do that.
10
by: Tony Stock | last post by:
Hi all, problem - need to read a log (text) file looking for certain entries, this file will still be written to by another 3rd party process, and hence constantly require monitoring (dare I say...
1
by: tfs | last post by:
I am reading into a datagrid and can't seem to get the "execution time" column (the 3rd column) to move to the right. My problem is that the last character in the 2nd column is right up against...
4
by: clickon | last post by:
I am building up the validation expression for a RegularExpresionValidator control by retreiving some values from a database and looping through the DataTable to build and adding lots of |...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
0
by: Anjo Gasa | last post by:
I have a couple questions regarding iostreams output. I just finished searching the group archive and don't believe they have been specifically address before. 1. How can one remove leading...
4
by: psbasha | last post by:
Hi, I have a string str1 = 'Pnt ', I would like to remove the spaces from the tailing end of a string. I/P : str1 = 'Pnt ' O/P : 'Pnt'
1
by: Shaw | last post by:
I have a number, for example 17.7 and I want to format to a string as "17.70", with the tailing zero. Using Math.Round( dValue, 2 ) - produces "17.7". Any other functions which will do what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...

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.