473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_no t_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_no t_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 6097
> 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_no t_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_no t_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_no t_of( "\t\n" ) );
// Trim trailing spaces ???
s.erase( s.find_first_no t_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_no t_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
2302
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 embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly replicate the input line) The error comes out in correspondance of a triple string.
1
17353
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
9020
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 tailing ?) Is it possible / practical / efficient to 'tail' a log file using c. Questions are :
1
1430
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 the end of the column and the 3rd column is too close and confusing. So I was trying to move the data in the 3rd column over a few spaces. So I did the following:
4
2464
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 | characters. Basically i want to build up a string along the lines of ^102|103|104|105$ where the number are coming from the data table. This is easy enough and works fine. Unfortunately the data values from the database are padded out with loads of...
5
2015
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 that looks like this:
0
1607
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 zero's from floating numbers. For example, consider this output: 0.2956097. Are there any manipulators that will set a stream to output .2956097 instead, removing the leading zero? 2. How can one control the width of the exponent field. For...
4
2438
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
1471
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 want, before I start writing formatting code? Thanks
0
9568
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
9404
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,...
1
9959
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
9837
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
8833
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
6651
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.