Solved

How to trim a string?

Posted on 2003-12-08
2
481 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 110
GUI: DIalog Stacking and Popping in MS C++ 4 96
Dynamically allocate memory 9 74
gdb doesn't stop on breakpoint 2 134
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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