大家好,我想問一個簡單的問題,就是CString使用Fomrat,想將引數次序調換,例如
szText.Format("this is %d %d",12,13);
最后的結果變成 this is 13 12 而不是 this is 12 13
按照網上的一些介紹
這樣寫 Format("this is %1:d %0:d",12,13);
但是總是報錯
->以下是網上的說明:
型別講述完畢,下面介紹格式化Type的指令:
[index ":"] 這個要怎么表達呢,看一個例子
Format("this is %d %d",12,13);
其中第一個%d的索引是0,第二個%d是1,所以字符顯示的時候
是這樣 this is 12 13
而如果你這樣定義:
Format("this is %1:d %0:d",12,13);
那么回傳的字串就變成了
this is 13 12
https://www.xuebuyuan.com/1609332.html
改成CString 的呼叫方法如下
CString szText;
szText.Format("this is %1:d %0:d",12,13);
AfxMessageBox(szText);
但是總是報錯,是什么原因呢,謝謝!

uj5u.com熱心網友回復:
是用FormatMessage()CString strText(TEXT(""));
strText.FormatMessage(TEXT("this is %2!d! %1!d!"), 12, 13);
AfxMessageBox(strText);https://docs.microsoft.com/en-us/cpp/atl-mfc-shared/reference/cstringt-class?view=vs-2019#format
uj5u.com熱心網友回復:
即%n!format string! Identifies an insert. The value of n can be in the range from 1 through 99. The format string (which must be surrounded by exclamation marks) is optional and defaults to !s! if not specified. For more information, see Format Specification Fields.
uj5u.com熱心網友回復:
謝謝回復,我剛試了一下,使用FormatMessage果然可以解決,但是浮點不知如何格式化?CString strText(TEXT(""));
strText.FormatMessage("this is %2!d! %1!d! %3% ", 12, 13 ,"我是測驗文本",5.25);
//此時 strText等于 "this is 13 12 我是測驗文本 f",而不是我想要的 "this is 13 12 我是測驗文本 5.25"
uj5u.com熱心網友回復:
!d! 是 整數 試試 !f!uj5u.com熱心網友回復:

試過了,沒用。
uj5u.com熱心網友回復:
Floating-point format specifiers—e, E, f, and g—are not supported. The workaround is to use the StringCchPrintf function to format the floating-point number into a temporary buffer, then use that buffer as the insert string.
uj5u.com熱心網友回復:
例如7.3f, 是3位小數的浮點數。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/37107.html
標籤:基礎類
上一篇:MFC控制元件
