473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clear spaces in a string

I have a string like that " Vertices "
and i want to obtain "Vertices"
I would want to know if there is a simple way to put off spaces on a
string, thank you

--
Marc Durufle
Inria Rocquencourt
Tel : 01 39 63 56 27
--------------------------
Jul 19 '05 #1
4 11170
"Marc Durufle" <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
I have a string like that " Vertices "
and i want to obtain "Vertices"
I would want to know if there is a simple way to put off spaces on a
string, thank you

--
Marc Durufle
Inria Rocquencourt
Tel : 01 39 63 56 27
--------------------------


Try this:
std::string s = " Vertices ";
std::string::iterator it = std::remove_if(s.begin(), s.end(),
std::bind2nd(std::equal_to<char>(), ' '));
s = std::string(s.begin(), it);
HTH,
S. Armondi
Jul 19 '05 #2

Marc Durufle <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
I have a string like that " Vertices "
and i want to obtain "Vertices"
I would want to know if there is a simple way to put off spaces on a
string, thank you

--
Marc Durufle
Inria Rocquencourt
Tel : 01 39 63 56 27
--------------------------


#include <iostream>
#include <sstream>
#include <string>

std::string strip(const std::string& s)
{
std::string result;
std::istringstream(s) >> result;
return result;
}

int main()
{
std::cout << '*'
<< strip(" Vertices ")
<< '*'
<< '\n';

return 0;
}

-Mike


Jul 19 '05 #3
Mike Wahler wrote:

std::istringstream(s) >> result;


Mike,

I don't think that will compile; you're passing a temporary by non-const
reference. I believe you have to do something like this:

std::istringstream iss(s);
iss >> result;

Regards,

Russell Hanneken
rh*******@pobox.com

Jul 19 '05 #4
"Marc Durufle" <Ma**********@inria.fr> wrote in message
news:3F***************@inria.fr...
I have a string like that " Vertices "
and i want to obtain "Vertices"
I would want to know if there is a simple way to put off spaces on a
string, thank you


Note: you should specify whether you intend to remove *all* spaces,
or just trim the ones at the beginning and the end of the string.
(several replies so far assume the former).

Here's a way to remove spaces at both ends of the string:
std::string trimmed( std::string const& str, char const* sepSet )
{
std::string::size_type const first = str.find_first_not_of(sepSet);
return ( first==std::string::npos )
? std::string()
: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}

NB: - str is the original string
- sepSet is the list of space characters.
Use for example: " " or " \t" or " \t\n\r" etc.
hth
--
Ivan Vecerina, Dr. med. <> http://www.post1.com/~ivec
Brainbench MVP for C++ <> http://www.brainbench.com

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
14972
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
9
6262
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
10
11091
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel...
7
3126
by: Eduardo78 | last post by:
Hello everybody, I heard I could clear (or move spaces) to all controls at ones without having to call each one of them. If someone remembers how to do that, I will apreciate your help. My...
135
7338
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
3
1175
by: Jan | last post by:
Hallo, How can i Clear empty spaces between text. For example: I've got the name "Justin Name" in a string and want to clear de spaces between de names so i will get "JustinName". What can...
15
15593
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
6
12924
by: Alexander Vasilevsky | last post by:
How to clear the screen in the console application in Microsoft Visual C #. NET 2003. http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas discount
8
4723
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.