I thought this would be really simple but it's presenting some difficulties. If I have
string name = "John"; int age = 21;
How do I combine them to get a single string "John21"?
I thought this would be really simple but it's presenting some difficulties. If I have
string name = "John"; int age = 21;
How do I combine them to get a single string "John21"?
Common Answer: itoa()
This is bad. itoa is non-standard, as pointed out in http://stackoverflow.com/questions/190229/where-is-the-itoa-function-in-linux
std::ostringstream o;
o << name << age;
std::cout << o.str();
If you have Boost, you can convert the integer to a string using boost::lexical_cast<std::string>(age)
.
Another way is to use stringstreams:
std::stringstream ss;
ss << age;
std::cout << name << ss.str() << std::endl;
A third approach would be to use sprintf
or snprintf
from the C library.
char buffer[128];
snprintf(buffer, sizeof(buffer), "%s%d", name.c_str(), age);
std::cout << buffer << std::endl;
Other posters suggested using itoa
. This is NOT a standard function, so your code will not be portable if you use it. There are compilers that don't support it.
#include <string>
#include <sstream>
using namespace std;
string concatenate(std::string const& name, int i)
{
stringstream s;
s << name << i;
return s.str();
}
It seems to me that the simplest answer is to use the sprintf function:
sprintf(outString,"%s%d",name,age);
Herb Sutter has a good article on this subject: "The String Formatters of Manor Farm". He covers Boost::lexical_cast, std::stringstream, std::strstream (which is deprecated), and sprintf vs. snprintf.
If you are using MFC, you can use a CString
CString nameAge = "";
nameAge.Format("%s%d", "John", 21);
Managed C++ also has a string formatter:
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
Then your usage would look something like this
std::string szName = "John";
int numAge = 23;
szName += to_string<int>(numAge);
cout << szName << endl;
Googled [and tested :p ]
The std::ostringstream is a good method, but sometimes this additional trick might get handy transforming the formatting to a one-liner:
#include <sstream>
#define MAKE_STRING(tokens) /****************/ \
static_cast<std::ostringstream&>( \
std::ostringstream().flush() << tokens \
).str() \
/**/
Now you can format strings like this:
int main() {
int i = 123;
std::string message = MAKE_STRING("i = " << i);
std::cout << message << std::endl; // prints: "i = 123"
}
EDITED: My former version was incorrectly written. Here's a quick one using the sprintf_s functionality.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[] = "John";
int age = 21;
char nameAndAge[50];
sprintf_s(nameAndAge,"%s%d",name,age);
cout << nameAndAge << endl;
return 0;
}
I don't have karma enough to comment (let alone edit), but Jay's post (currently the top-voted one at 27) contains an error. This code:
std::stringstream ss;
ss << age;
std::cout << name << ss.str() << std::endl;
Does not solve the stated problem of creating a string consisting of a concatenated string and integer. I think Jay meant something more like this:
std::stringstream ss;
ss << name;
ss << age;
std::cout << "built string: " << ss.str() << std::endl;
The final line is just to print the result, and shows how to access the final concatenated string.
If using sstream for completeness refer to http://stackoverflow.com/questions/20731/in-c-how-do-you-clear-a-stringstream-variable. Continued use of the stringstream variable causes grief, e.g.
char loop = 'n'; //y to continue looping
int count = 1;
stringstream out;
string prefix;
do
{
out << count;
prefix = out.str();
//out.str("");//uncomment to clear the stream
cout << prefix << endl;
cout << "Continue (y/n): ";
cin >> loop;
cout << endl;
count++;
}while(tolower(loop) == 'y');
In alphabetical order:
std::string name = "John"; int age = 21;
std::string result;
// 1. with Boost
result = name + boost::lexical_cast<std::string>(age).
// 2. with FastFormat.Format
fastformat::fmt(result, "{0}{1}", name, age);
// 3. with FastFormat.Write
fastformat::write(result, name, age);
// 4. with IOStreams
std::stringstream sstm;
sstm << name << age;
result = sstm.str();
// 5. with itoa
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + itoa(age, numstr, 10);
// 6. with sprintf
char numstr[21]; // enough to hold all numbers up to 64-bits
sprintf(numstr, "%d", age);
result = name + numstr;
// 7. with STLSoft's integer_to_string
char numstr[21]; // enough to hold all numbers up to 64-bits
result = name + stlsoft::integer_to_string(numstr, 21, age);
// 8. with STLSoft's winstl::int_to_string()
result = name + winstl::int_to_string(age);
There are more options possible to use to concatenate integer (or other numerric object) with string. It is Boost.Format
#include <boost/format.hpp>
#include <string>
int main()
{
using boost::format;
int age = 22;
std::string str_age = str(format("age is %1%") % age);
}
and Karma from Boost.Spirit (v2)
#include <boost/spirit/include/karma.hpp>
#include <iterator>
#include <string>
int main()
{
using namespace boost::spirit;
int age = 22;
std::string str_age("age is ");
std::back_insert_iterator<std::string> sink(str_age);
karma::generate(sink, int_, age);
return 0;
}
Boost.Spirit Karma claims to be one of the fastest option for integer to string conversion.