Solved

How to trim a string?

Posted on 2003-12-08
2
476 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
  • 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 50 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now