Create string with format in v3?

Hi!

How can i create a string with format in v3, for instance a scoreLabel?

Thanks!

Multiple ways.

Technically StringUtils::format exists in the deprecated source file CCString.cpp, but it works and if they remove it just find a copy of it in the git history and add to your project’s utils methods.

StringUtils::format("number = %d", 7);

stringstream is one of the standard c++ ways

std::stringstream ss;
ss << "number = " << 7;
std::string s = ss.str();

If you are only formatting by replacing %s with strings you can use string concatenation.

std::string prefix("number");
std::string s = prefix + " = " + to_string(7);

Then just pass the std::string to the Label’s setString() method.

Thank you! But which one do you use/prefer?

hi @nichlaspro132

It really depends on your choice and the best use in the required scenario.
However, I mostly use the second method and it works pretty good for me.

Happy Coding :smile:

1 Like