C++ Builder 参考手册 ➙ System::Sysutils ➙ StrFmt
格式化数据到字符串
头文件:#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 参数的函数是线程安全的,请参考 FormatSettings 和 TFormatSettings;
- 这个函数内部通过调用 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
}
运行结果:
运行结果
相关:
- System::Sysutils::Format
- System::Sysutils::FormatBuf
- System::Sysutils::FormatCurr
- System::Sysutils::FormatDateTime
- System::Sysutils::FormatFloat
- System::Sysutils::FormatSettings
- System::Sysutils::FmtStr
- System::Sysutils::FmtLoadStr
- System::Sysutils::StrFmt
- System::Sysutils::StrLFmt
- System::Sysutils::TFormatSettings
- System::Sysutils::WideFormat
- System::Sysutils::WideFormatBuf
- System::Sysutils::WideFmtStr
- System::Sysutils
- std::printf, std::_tprintf, std::wprintf
- std::sprintf, std::_stprintf, std::swprintf
- std::vprintf, std::_vtprintf, std::vwprintf
- std::vsprintf, std::_vstprintf, std::vswprintf
- std::snprintf, std::_sntprintf, std::snwprintf
- std::vsnprintf, std::_vsntprintf, std::vsnwprintf
- <cstdio>
C++ Builder 参考手册 ➙ System::Sysutils ➙ StrFmt