如何将 C 样式的 printf 格式转换为 C# 字符串格式? [英] How can I convert a C-style printf format to a C# string format?

查看:28
本文介绍了如何将 C 样式的 printf 格式转换为 C# 字符串格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串格式模式从 C 的 printf() 转换为 C# string.Format() 模式?

How can I convert the string formatting pattern from C's printf() to C# string.Format() pattern?

例如 C 的 "%04x" 转换为 C# 的 "X4"

For instance C's "%04x" converts to C#'s "X4"

推荐答案

我认为将一种字符串模式转换为另一种字符串模式最好使用正则表达式.(如果问题是通用的,或者可能仅限于几个感兴趣的部分,那将是我的方法.)

Converting one string pattern to another string pattern is best achieved with regex I think. (That would be my approach if the problem is generic or perhaps limited to a few sections of interest.)

如果您在 .net 中使用 printf,则可以使用 lib 解决:http://www.codeproject.com/Articles/19274/A-printf-implementation-in-Chttps://sourceforge.net/projects/printfnet/

If you're after the printf in .net, it could be solved with a lib: http://www.codeproject.com/Articles/19274/A-printf-implementation-in-C https://sourceforge.net/projects/printfnet/

或者使用 pinvoke 到真正的 printf:http://community.bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx

Or with pinvoke to the real printf: http://community.bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx

将 printf 模式转换为 string.format 的通用方法或许可以从两个(开源)库之一中提取.

A generic approach to converting a printf pattern to string.format could perhaps be extracted from one of the two (open source) libs.

这篇关于如何将 C 样式的 printf 格式转换为 C# 字符串格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆