Skip to content

Features: .Net String Formatting for Cpp

Rudy Huyn edited this page Aug 7, 2019 · 2 revisions

Differences between C++ and .Net String formatting

String formatting in C++ is quite different than in C#/VB.Net.

C#/VB.Net: Hello {0}, you got {1} messages!

C++: Hello %ls, you got {u} messages!

Not only you need to strongly type your template (%d, %s instead of {0}, {1}), but you also need work with fixed size string buffers, you can't directly manipulate Platform::String objects and can't convert automatically your objects to strings.

Another issue is that you can't share the string templates in your resource files with a C#/VB.Net library.

.Net String Formatting in your localization

To simplify your code, ReswPlus allows you to use .Net string templates in your C++ projects, the library handling the string formatting for you, simply use the tag #FormatNet instead of #Format and ReswPlus will correctly parse your .Net String templates.

Here is a example showing the difference between the 2 ways possible for a C++ project

C++ String formatting:

Key Value Comment
ForecastAnnouncement The temperature in %1$ls is %0$d degrees #Format[Int32 temp, String city]

.Net String formatting:

Key Value Comment
ForecastAnnouncement The temperature in {1} is {0} degrees #FormatNet[Int32 temp, String city]

Use the .Net String formatter in your code

If you want to use the .Net String Formatter provided by ReswPlusLib in your application, you can write:

ReswPlusLib::StringFormatting::FormatDotNet(string, params...);