C++ Builder 参考手册 ➙ System::Sysutils ➙ StrLFmt
格式化数据到字符串
头文件:#include
命名空间:System::Sysutils
函数原型:
char *__fastcall StrLFmt(
char *Buffer,
unsigned MaxBufLen,
char *Format,
const System::TVarRec *Args,
const int Args_High);
char *__fastcall StrLFmt(
char *Buffer,
unsigned MaxBufLen,
char *Format,
const System::TVarRec *Args,
const int Args_High,
const TFormatSettings &AFormatSettings);
System::WideChar *__fastcall StrLFmt(
System::WideChar *Buffer,
unsigned MaxBufLen,
System::WideChar *Format,
const System::TVarRec *Args,
const int Args_High);
System::WideChar *__fastcall StrLFmt(
System::WideChar *Buffer,
unsigned MaxBufLen,
System::WideChar *Format,
const System::TVarRec *Args,
const int Args_High,
const TFormatSettings &AFormatSettings);
参数:
- Buffer:用于返回生成的字符串;
- MaxBufLen:生成字符串的最大长度;
- Format:输出数据的格式;
- Args:要输出的数据数组;
- Args_High:数据的个数减1;
- AFormatSettings:地区格式;
返回值:
- 按照 Format 参数的格式输出参数 Args 数据到 Buffer 字符串,函数直接返回 Buffer;
- Buffer 需要有 MaxBufLen + 1 个字符的内存空间,因为生成的字符串后面的结束符不包含在 MaxBufLen 之内;
- 这个函数的参数及生成的字符串规则和 Format 函数相同,详细规则请参考 Format 函数。
- 没有 AFormatSettings 参数的函数不是线程安全的,因为使用了全局变量作为地区格式;带有 AFormatSettings 参数的函数是线程安全的,请参考 FormatSettings 和 TFormatSettings;
- Args, Args_High 可以使用 ARRAYOFCONST 宏,请参考本文后面的例子;
- 这个函数和 StrFmt 的区别:这个函数有 MaxBufLen 参数,而 StrFmt 没有。
比较以下两个例子,其中:
- Format 函数的模板版本使用最简单;
- 其次是 Format、StrFmt 和 StrLFmt 这三个函数使用 ARRAYOFCONST 宏;
- 直接使用 TVarRec 数组最麻烦。
例1:使用函数 Format、StrFmt 和 StrLFmt 输出1个整数、1个浮点数和一个字符串,这三个函数都使用 ARRAYOFCONST 宏:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i = 123;
double x = 4.5678;
const wchar_t *t = L"Hello, Hsuanlu!";
Memo1->Lines->Add(Sysutils::Format(L"i=%d, x=%.3f, s=\"%s\"", ARRAYOFCONST((i,x,t))));
wchar_t s[1024];
wchar_t fmt[] = L"i=%d, x=%.3f, s=\"%s\"";
Sysutils::StrFmt(s, fmt, ARRAYOFCONST((i,x,t)));
Memo1->Lines->Add(s); // i=123, x=4.568, s="Hello, Hsuanlu!"
Sysutils::StrLFmt(s, 20, fmt, ARRAYOFCONST((i,x,t)));
Memo1->Lines->Add(s); // i=123, x=4.568, s="H
}
运行结果:
例1运行结果
例2:使用函数 Format、StrFmt 和 StrLFmt 输出1个整数、1个浮点数和一个字符串,其中 Format 使用模板版本,StrFmt 和 StrLFmt 直接使用 TVarRec 数组:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i = 123;
double x = 4.5678;
const wchar_t *t = L"Hello, Hsuanlu!";
Memo1->Lines->Add(Sysutils::Format(L"i=%d, x=%.3f, s=\"%s\"", i, x, t));
wchar_t s[1024];
wchar_t fmt[] = L"i=%d, x=%.3f, s=\"%s\"";
System::TVarRec a[] = {i,x,t};
Sysutils::StrFmt(s, fmt, a, 2);
Memo1->Lines->Add(s); // i=123, x=4.568, s="Hello, Hsuanlu!"
Sysutils::StrLFmt(s, 20, fmt, a, 2);
Memo1->Lines->Add(s); // i=123, x=4.568, s="H
}
运行结果:
例2运行结果
相关:
- 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
C++ Builder 参考手册 ➙ System::Sysutils ➙ StrLFmt