Jump to content
  • Advertisement
Sign in to follow this  
Ultraseamus

add an int to a string

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function that takes a string, and i need to pass it a string that has an unsigned int in the middle of it. so if unsigned value was 3, i would need to make the string: "the value of the number is (3)" Thanks to anyone that can help me with this.

Share this post


Link to post
Share on other sites
Advertisement
Boost:

std::string s = "the value of the number is " + boost::lexical_cast<std::string>(3);

STL:

#include <sstream>

std::ostringstream sstr;
sstr << "the value of the number is ";
sstr << 3;

std::string s = sstr.str();

Or you could go an mostly C route:

char str[100];

sprintf(str, "the value of the number is %d", 3);

std::string s = str;

Share this post


Link to post
Share on other sites
If you're using C, you can use snprintf() which is just like printf but it writes into a string.

If you're using C++ you can use a stringstream like so.


#include <sstream>

std::ostringstream ouputString;

outputString << "the value of the number is (" << 3 << ")";

// call your function
func( outputString.str() );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!