如何在与string.format一起使用的c#字符串中转换CString(例如“test%s”)?
例如
CString outStr;
CString formatstring = "test %s"; // this string came from an external file
CString str = "Some Data";
outStr.Format(formatstring, str); // this works in MFC
// outStr -> "test Some Data"
我想使用c#
实现相同的outStr结果"test %s"; //the formatted string coming from an external file
// in c# if I understand well it should be "test {0}
CString str = "Some Data";
outStr.Format(formatstring, str); //in c# this doesn't work
// outStr -> "test %s"
希望这可以澄清 搜索函数或转换更复杂的CString的方法。
答案 0 :(得分:-1)
C#中的字符串格式称为复合格式,与C和C ++字符串格式略有不同。以下是Microsoft的文档...
这是一个有效的C#示例,它在功能上等同于您的MFC CString示例...
static void Main(string[] args)
{
String formatstring = "test {0}";
String str = "Some Data";
String outStr;
outStr = String.Format(formatstring, str);
Console.WriteLine(outStr);
}
这个例子将编写"测试一些数据"到控制台。