StrFmt
Format a string with a format specifier and a variable number of parameters.
string StrFmt(const char* pszFmt, ...); void StrFmt(string& str, const char* pszFmt, ...);
Parameters
- pszFmt - character array of formatting information
- str - reference to string to provide output
- ... - variable number of parameters to fill in formatting specifiers
Return Value
- First version returns the formatted string. Second version does not return anything since the formatting passed string reference is used to get the result.
Remarks
This is basically a wrapper around sprintf to provide the flexibility of formatting with some safety measures built in. If string parameters are used the ".c_str()" method will be needed to convert this to C style strings. Refer to http://www.cplusplus.com/reference/clibrary/cstdio/printf/ for formatting specifiers.
Example
int nVal = 123; string strID = "ABC"; string strMsg; strMsg = StrFmt("Number: %d ID: %s", nVal, strID.c_str());
The version of StrFmt shown below that takes a reference to the string as a parameter is more efficient. This is because it doesn't have to create the string, and pass it as a return value to tbe recreated.
int nVal = 123; string strID = "ABC"; string strMsg; StrFmt(strMsg, "Number: %d ID: %s", nVal, strID.c_str());
See Also
Last modified
20 months ago
Last modified on 04/13/23 07:01:23
Note:
See TracWiki
for help on using the wiki.