473,666 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting Rid of Trailing white spaces

Hi, I need help getting rid of trailing white spaces. I am searching a
file for various data (not important) and retrieving it. I output the
data if found to a file. An example would be:
HD='Three blind mice '
My output would consist of:
Three Blind Mice (w/ all the white space behind being printed until the
quote mark is reached)

I don't the white space after the text to be printed.

How do I get rid of trailing white spaces?

Jul 23 '05 #1
4 3211
ucfcpegirl06 wrote:
Hi, I need help getting rid of trailing white spaces. I am searching a
file for various data (not important) and retrieving it. I output the
data if found to a file. An example would be:
HD='Three blind mice '
My output would consist of:
Three Blind Mice (w/ all the white space behind being printed until the
quote mark is reached)

I don't the white space after the text to be printed.

How do I get rid of trailing white spaces?


A combination of 'find_last_not_ of' and 'substr' should help.

V
Jul 23 '05 #2

"ucfcpegirl 06" <tr************ **@gmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi, I need help getting rid of trailing white spaces. I am searching a
file for various data (not important) and retrieving it. I output the
data if found to a file. An example would be:
HD='Three blind mice '
My output would consist of:
Three Blind Mice (w/ all the white space behind being printed until the
quote mark is reached)

I don't the white space after the text to be printed.

How do I get rid of trailing white spaces?


You've not stated how you are storing the string. The example provided is
not a string (ie: 'c' vs "a string"). Here is an example of stripping spaces
using a std::string:

#include <iostream>
#include <string>

std::string stripspaces(con st std::string& r_s)
{
int nstart = r_s.find_first_ not_of(' ');
int nend = r_s.find_last_n ot_of(' ') - 1;

std::string s_result = r_s.substr(nsta rt, nend);
return s_result;
}

int main()
{
std::string s(" a simple string ");
std::cout << "original string\n\t\tsta rt->" << s;
std::cout << "<-end" << std::endl;

std::cout << "stripped string\n\t\tsta rt->" << stripspaces(s);
std::cout << "<-end" << std::endl;

return 0;
}

/* output:

original string
start-> a simple string <-end
stripped string
start->a simple string<-end

*/

Jul 23 '05 #3
Peter Julian wrote:
int nend = r_s.find_last_n ot_of(' ') - 1;
Are you sure this does what you want if the string consists of only
spaces?

std::string s_result = r_s.substr(nsta rt, nend);

Jul 23 '05 #4
Is there a way to do this w/o using strings? I'm using ptrs to search
the file and output the necessary text.

Peter Julian wrote:
"ucfcpegirl 06" <tr************ **@gmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi, I need help getting rid of trailing white spaces. I am searching a
file for various data (not important) and retrieving it. I output the
data if found to a file. An example would be:
HD='Three blind mice '
My output would consist of:
Three Blind Mice (w/ all the white space behind being printed until the
quote mark is reached)

I don't the white space after the text to be printed.

How do I get rid of trailing white spaces?


You've not stated how you are storing the string. The example provided is
not a string (ie: 'c' vs "a string"). Here is an example of stripping spaces
using a std::string:

#include <iostream>
#include <string>

std::string stripspaces(con st std::string& r_s)
{
int nstart = r_s.find_first_ not_of(' ');
int nend = r_s.find_last_n ot_of(' ') - 1;

std::string s_result = r_s.substr(nsta rt, nend);
return s_result;
}

int main()
{
std::string s(" a simple string ");
std::cout << "original string\n\t\tsta rt->" << s;
std::cout << "<-end" << std::endl;

std::cout << "stripped string\n\t\tsta rt->" << stripspaces(s);
std::cout << "<-end" << std::endl;

return 0;
}

/* output:

original string
start-> a simple string <-end
stripped string
start->a simple string<-end

*/


Jul 23 '05 #5

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

Similar topics

4
22843
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely. This means I need to append trailing spaces to a text string (such as "Test1 ") or append leading space in front of a text string that contains a number (such as " 12.00"). Now, the stored procedure works fine when I run it in Query Analyzer....
7
7416
by: ITM | last post by:
Can anyone tell me how I can prevent Access adding trailing spaces when I insert a value into a Text column? For example, if I execute the following statement: UPDATE Log SET Log.Title = 'Testing' WHERE Log.Identifier=1; ....then the following statement:
5
5427
by: Tammy | last post by:
I am doing some genealogy research and I have discovered that there is a lot of data available on the web in text format. The problem is that the columns in the files are lined up by spaces. I'd like to be able to import these files into Access or Excel so that they can be sorted. What I plan on doing is adding in commas at the end of each field to create a csv file. What I need to know is how can I easily remove the spaces between...
5
3358
by: Stan Shankman | last post by:
How do I add trailing spaces within a right-justified textBox? In order to add a trailing space to the text in a right-justified textBox, I use to just append a space to the end of the text string. But now, textBox seems to have gotten smarter, as it automatically remove trailing spaces. So the question remains: How now do I add trailing spaces to text inside a right-justified textBox?
0
1934
by: Seth | last post by:
First off, my apologies if this is in the wrong newsgroup, but I hope I'm close enough. I'm trying to do some parsing of a CSV file using OleDbConnection, but for some reason, when I populate my DataSet, it is trimming the trailing spaces. Anybody know why? Here is my code: System.Data.OleDb.OleDbConnection connection = null;
2
3311
by: rsine | last post by:
I am developing a program that requires me to read a string of data from a text field in a database. Data is parsed from the string based upon a starting position/length. If a piece of data doesn't meet the required field length, then spaces are added, thus my issue. It appears VB.net is trimming any trailing spaces. What should be a 250 character data string is only 243 (less 7 trailing spaces). Is there a way to get this to stop? I...
17
11856
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can I force Access not to truncate?! Thanx, /Toommy
5
4232
by: TGEAR | last post by:
Claim number (string) CF060001 CF060001A CF060001B AV000001 AV000212F AV000001F FD232122 FD232122G SD223213
5
2955
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not SQL Server 2005. Anybody know if this change in behavior is documented? If it is intentional? Is there a "quick fix" to revert to the old behavior (to automatically RTRIM the results of SYSTEM_USER in 2005) until code can be changed?
0
8448
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
8356
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
8552
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
8640
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
7387
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
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
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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.