Getting Rid of Trailing white spaces

U

ucfcpegirl06

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?
 
V

Victor Bazarov

ucfcpegirl06 said:
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
 
P

Peter Julian

ucfcpegirl06 said:
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

*/
 
U

ucfcpegirl06

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top