StrFmt - C++ Builder

C++ Builder 参考手册System::SysutilsStrFmt


格式化数据到字符串

头文件:#include <System.SysUtils.hpp>
命名空间:System::Sysutils
函数原型:

char * __fastcall StrFmt(char * Buffer, char * Format, const System::TVarRec *Args, const int Args_High);
char * __fastcall StrFmt(char * Buffer, char * Format, const System::TVarRec *Args, const int Args_High, const TFormatSettings &AFormatSettings);
System::WideChar * __fastcall StrFmt(System::WideChar * Buffer, System::WideChar * Format, const System::TVarRec *Args, const int Args_High);
System::WideChar * __fastcall StrFmt(System::WideChar * Buffer, System::WideChar * Format, const System::TVarRec *Args, const int Args_High, const TFormatSettings &AFormatSettings);

参数:

  • Buffer:用于返回生成的字符串;
  • Format:输出数据的格式;
  • Args:要输出的数据数组;
  • Args_High:数据的个数减1;
  • AFormatSettings:地区格式;

返回值:

  • 按照 Format 参数的格式输出参数 Args 数据到 Buffer 字符串,函数直接返回 Buffer;
  • 参数 Buffer 需要有足够的空间储存生成的字符串,函数不检查是否预先分配了足够的空间;
  • 这个函数的参数及生成的字符串规则和 Format 函数相同,详细规则请参考 Format 函数。
  • 没有 AFormatSettings 参数的函数不是线程安全的,因为使用了全局变量作为地区格式;带有 AFormatSettings 参数的函数是线程安全的,请参考 FormatSettingsTFormatSettings
  • 这个函数内部通过调用 FormatBuf 实现的,所以这个函数和 FormatBuf 函数功能相同,只是参数不同;

例子:测试使用 Format 和 StrFmt 输出一个整数和一个浮点数值

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int i = 123;
    double x = 4.5678;
    Memo1->Lines->Add(Sysutils::Format(L"i=%d, x=%.3f", i, x));

    System::TVarRec a[] = {i,x};
    wchar_t fmt[] = L"i=%d, x=%.3f";
    wchar_t s[1024];
    Sysutils::StrFmt(s, fmt, a, 1);
    Memo1->Lines->Add(s); // i=123, x=4.568
}

运行结果:

运行结果

相关:


C++ Builder 参考手册System::SysutilsStrFmt

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容