Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "Bloke %s drank %5.2f litres of booze and ate %d bananas"
- "Bloke {0} drank {1,5:f2} litres of booze and ate {2} bananas"
- #include <string.h>
- void convertCtoCSharpFormat(char *dst, const char *src) {
- int k1 = 0, k2 = 0;
- while (*src) {
- while (*src && (*src != '%')) *dst++ = *src++;
- if (*src == '%') {
- const char *percent;
- src++;
- if (*src == '%') { *dst++ = '%'; continue; }
- if (*src == 0) { /* error: unmatched % */; *dst = 0; return; }
- percent = src;
- /* ignore everything between the % and the conversion specifier */
- while (!strchr("diouxXeEfFgGaAcpsn", *src)) src++;
- /* replace with {k} */
- *dst++ = '{';
- if (k2) *dst++ = k2 + '0';
- *dst++ = k1++ + '0';
- if (k1 == 10) { k2++; k1 = 0; }
- /* *src has the conversion specifier if needed */
- /* percent points to the initial character of the conversion directive */
- if (*src == 'f') {
- *dst++ = ',';
- while (*percent != 'f') *dst++ = *percent++;
- }
- *dst++ = '}';
- src++;
- }
- }
- *dst = 0;
- }
- #ifdef TEST
- #include <stdio.h>
- int main(void) {
- char test[] = "Bloke %s drank %5.2f litres of booze and ate %d bananas";
- char out[1000];
- convertCtoCSharpFormat(out, test);
- printf("C fprintf string: %snC# format string: %sn", test, out);
- return 0;
- }
- #endif
- StringBuilder cString = new StringBuilder("Bloke %s drank %5.2f litres of booze and ate %d bananas");
- cString.Replace("%s","{0}");
- cString.Replace("%5.2f", "1,5:f2"); // I am unsure of this format specifier
- cString.Replace("%d", "{2}");
- string newString = String.Format(cString.ToString(), var1, var2, var3);
Add Comment
Please, Sign In to add comment