Remove spaces from a string in C++ and convert into integer.

How to remove spaces from a string in C++ and convert it into integer in a simple way.

string spaces can be easily remove using :::

->>remove_if(str.begin(), str.end(), isspace);

->>str.erase(remove(str.begin(),str.end(),’ '),str.end());

convert string into numbers:::

1.)

#include "sstream"

// st is input string

int result;

stringstream(st) >> result;

2.)

std::string myString = "45";

int value = atoi(myString.c_str()); //value = 45 

3.)

ifastream<basic_formatters, string_reader> myString(&string);

int value;

myString >> value;

4.)

#include"sstream"

#include "string"

using namespace std;

string myStream = "45";

istringstream buffer(myString);

int value;

buffer >> value;   // value = 45 

hope this help :slight_smile:

2 Likes