By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Getting Rid of Trailing white spaces

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"ucfcpegirl06" <tr**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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(const std::string& r_s)
{
int nstart = r_s.find_first_not_of(' ');
int nend = r_s.find_last_not_of(' ') - 1;

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

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

std::cout << "stripped string\n\t\tstart->" << 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

P: n/a
Peter Julian wrote:
int nend = r_s.find_last_not_of(' ') - 1;
Are you sure this does what you want if the string consists of only
spaces?

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

Jul 23 '05 #4

P: n/a
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:
"ucfcpegirl06" <tr**************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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(const std::string& r_s)
{
int nstart = r_s.find_first_not_of(' ');
int nend = r_s.find_last_not_of(' ') - 1;

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

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

std::cout << "stripped string\n\t\tstart->" << 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 discussion thread is closed

Replies have been disabled for this discussion.