CString::Format

CString::Format

// 看代码,针对format带参数列表的情况没整明白,于是开始百度,搜索百度一大圈没找到好的解答,最后还是微软靠谱,与大家分享。

voidFormat(LPCTSTRlpszFormat**, ...);**

void Format( UINT nFormatID**, ... );**

Parameters

lpszFormat

A format-control string.

nFormatID

The string resource identifier that contains the format-control string.

Remarks

Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.

The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:

CString str = "Some Data";
str.Format("%s%d", str, 123);   // Attention: str is also used in the parameter list.

will cause unpredictable results.

When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, see in the Run-Time Library Reference.) A null character is appended to the end of the characters written.

For more information, see in the Run-Time Library Reference.

Example

CString str;

str.Format(_T("Floating point: %.2f\n"), 12345.12345);
_tprintf("%s", (LPCTSTR) str);

str.Format(_T("Left-justified integer: %.6d\n"), 35);
_tprintf("%s", (LPCTSTR) str);

str.Format(IDS_SCORE, 5, 3);
_tprintf("%s", (LPCTSTR) str);
  

Output

If the application has a string resource with the identifier IDS_SCORE that contains the string "Penguins: %d\nFlyers  : %d\n", the above code fragment produces this output:

Floating point: 12345.12
Left-justified integer: 000035
Penguins: 5
Flyers  : 3

CString Overview |  Class Members |  Hierarchy Chart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值