Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format for C++

Looking for an implementation for C++ of a function like .NET's String.Format. Obviously there is printf and it's varieties, but I'm looking for something that is positional as in:

String.Format("Hi there {0}. You are {1} years old. How does it feel to be {1}?", name, age);

This is needed because we're going to try and make it easier to localize our app, and giving the translators {0} and {1} to position anywhere in the sentence is much easier than giving them a %s, %d, %d which must be positioned in that order in their translation.

I suppose search and replace with variable inputs (va_start, va_end, etc) is what I'll end up building, but if there is already a solid solution, that would be preferrable.

Thanks :)

like image 311
DougN Avatar asked Jan 20 '09 18:01

DougN


People also ask

What is %s and %D in C?

%s is for string %d is for decimal (or int) %c is for character.

What is %f %s in C?

The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.

What is the format of string?

String formatting is also known as String interpolation. It is the process of inserting a custom string or variable in predefined text. As a data scientist, you would use it for inserting a title in a graph, show a message or an error, or pass a statement to a function.

What is %z in printf?

We should use “%zu” to print the variables of size_t length. We can use “%d” also to print size_t variables, it will not show any error. The correct way to print size_t variables is use of “%zu”. In “%zu” format, z is a length modifier and u stand for unsigned type.


2 Answers

Look at the boost format library.

like image 168
Eddie Parker Avatar answered Sep 20 '22 18:09

Eddie Parker


QT's QString allows you do to this:

QString("Hi there %1. You are %2 years old. How does it feel \
         to be %2?").arg(name).arg(age)
like image 42
shoosh Avatar answered Sep 21 '22 18:09

shoosh