?
Solved

How to trim a string?

Posted on 2003-12-08
2
Medium Priority
?
483 Views
Last Modified: 2010-04-01
We know in Java or in C# onecan trim a string. Is there any function which can do th same?

For example:

...
...
string s1;
...
...

in run time, s1 has a value of "hello world   ". I want to trim the spaces to have "hello world". Surely there many methods to do this. But I want a simple way.
0
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9895914
s1.erase ( s1.find_last_not_of (" ") + 1 ) ;

You can add more characters to the " " string, which specifies which characters to trim off the end. If you for example also want to trim tabs, use " \t".
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 200 total points
ID: 9895934
BTW: If you also want to trim the string on the left side, there is also the find_first_not_of() method.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question