关于 AnsiString 的 Format 问题

WorldCreater 2000-12-06 10:38:00
加精
如何使用 AnsiString 的 Format 函数
...全文
1321 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
Againstwind 2001-06-19
  • 打赏
  • 举报
回复
「已注销」 2001-05-20
  • 打赏
  • 举报
回复
楼上写的非常精彩

贴主应该加分
Phoenix2000 2000-12-25
  • 打赏
  • 举报
回复
我在这里讲解一下 C++Builder 中的 Format 函数及其用法。

C++Builder 中的 Format 函数作用与 Delphi 中的是一样的,相信不用我废话了。但是唯一不同的是参数。由于 Delphi 支持“开参数”和动态数组特性,所以 Delphi 中的 Format 只有两个参数,其中后一个参数就是一个动态开参数常数数组。请注意我的用词!Delphi 的动态开参数常数数组的类型声明是 array of const,这种动态常数开数组的每一个元素可以是不同的类型。而且使用时的语法类似 Delphi 中的集合:采用两个方括号把不同类型的变量括起来,也可以采用声明一个 TVarRec 类型的数组来容纳不同的类型变量(具体请参阅其帮助文档)。而在 C++Builder 中,没有像 Delphi 中的那中语法特性,BCB 中的 Format 函数其第一个参数与 Delphi 的 Format 是类似的(一个格式化描述字符串),而后面有两个参数,第二个参数是一个 TVarRec 类型的数组指针,第三个参数是这个数组的最后一个元素的索引值!因为 C++ 不支持动态数组语法,也没有什么“开参数”之类的怪异特性,所以当你传递数组时,你必须同时传递数组的大小(Format 要求的是数组的最后一个元素的索引值,因此大同小异)。刚才我说过,在 Delphi 中也可以用 TVarRec 数组代替方括号的方法,其实本质是一样的。当然了,在 C++Builder 中,我们没有必要在每次使用 Format 函数时都先声明一个 TVarRec 数组,然后再把要用于输出的变量赋给 TvarRec 数组的每一个元素,最后还要传递 TVarRec 数组的最后元素索引。其实,我们可以使用一个 C++Builder 预先声明好的宏 ARRAYOFCONST,直接传入要格式化输出的变量,请看下面的例子:

Memo1->Text = Format(
"My name is %s, I'm %d years old.",
ARRAYOFCONST(("Phoenix2000", 22))
);

OK, 一切就搞定了!这个宏的实质是传递两个参数给 Format 函数,一个是 TVarRec 数俎指针,另一个是这个数俎的最后元素的索引,大家可以参照一下有关帮助文档。另外,C++Builder 中的 AnsiString 类也有一个 Format 类成员函数,不过这个成员函数是个静态函数,使用时要注意一下。好了,欢迎联系:zhx2001@21cn.com
Phoenix2000 2000-12-25
  • 打赏
  • 举报
回复
我在这里讲解一下 C++Builder 中的 Format 函数及其用法。

C++Builder 中的 Format 函数作用与 Delphi 中的是一样的,相信不用我废话了。但是唯一不同的是参数。由于 Delphi 支持“开参数”和动态数组特性,所以 Delphi 中的 Format 只有两个参数,其中后一个参数就是一个动态开参数常数数组。请注意我的用词!Delphi 的动态开参数常数数组的类型声明是 array of const,这种动态常数开数组的每一个元素可以是不同的类型。而且使用时的语法类似 Delphi 中的集合:采用两个方括号把不同类型的变量括起来,也可以采用声明一个 TVarRec 类型的数组来容纳不同的类型变量(具体请参阅其帮助文档)。而在 C++Builder 中,没有像 Delphi 中的那中语法特性,BCB 中的 Format 函数其第一个参数与 Delphi 的 Format 是类似的(一个格式化描述字符串),而后面有两个参数,第二个参数是一个 TVarRec 类型的数组指针,第三个参数是这个数组的最后一个元素的索引值!因为 C++ 不支持动态数组语法,也没有什么“开参数”之类的怪异特性,所以当你传递数组时,你必须同时传递数组的大小(Format 要求的是数组的最后一个元素的索引值,因此大同小异)。刚才我说过,在 Delphi 中也可以用 TVarRec 数组代替方括号的方法,其实本质是一样的。当然了,在 C++Builder 中,我们没有必要在每次使用 Format 函数时都先声明一个 TVarRec 数组,然后再把要用于输出的变量赋给 TvarRec 数组的每一个元素,最后还要传递 TVarRec 数组的最后元素索引。其实,我们可以使用一个 C++Builder 预先声明好的宏 ARRAYOFCONST,直接传入要格式化输出的变量,请看下面的例子:

Memo1->Text = Format(
"My name is %s, I'm %d years old.",
ARRAYOFCONST(("Phoenix2000", 22))
);

OK, 一切就搞定了!这个宏的实质是传递两个参数给 Format 函数,一个是 TVarRec 数俎指针,另一个是这个数俎的最后元素的索引,大家可以参照一下有关帮助文档。另外,C++Builder 中的 AnsiString 类也有一个 Format 类成员函数,不过这个成员函数是个静态函数,使用时要注意一下。好了,欢迎联系:zhx2001@21cn.com
WorldCreater 2000-12-24
  • 打赏
  • 举报
回复
我有点失望
清那位给我解释一下
format
函数的三个参数的含义及用法
好吗
这样这个问题就可以结束了
yjz 2000-12-13
  • 打赏
  • 举报
回复
LabelSpeed->Caption=Format("%d / %d",OPENARRAY(TVarRec,(TransferBytes,MSecondDelay)));


http://xayyy.go.163.com
WorldCreater 2000-12-12
  • 打赏
  • 举报
回复
这个方法我也会
但是好象 format 也能达到这种效果的
comanche 2000-12-11
  • 打赏
  • 举报
回复
好吧好吧,想了半天也想不到可以一条语句就完成的可能,只有这样了
兄弟们看到了不要笑我才是
AnsiString Temp = Test.SubString( 1, 3 );
Temp += "-";
Temp += Test.SubString( 3, 3 );
Temp += "-';
Temp += Test SubString( 6, 3 );
除此之外我也不知道有什么办法了
Winsky 2000-12-08
  • 打赏
  • 举报
回复
或者
TDate NowDate;
NowDate = ::Date();
NowDate.FormatString("yyyy-mm-dd");
王集鹄 2000-12-08
  • 打赏
  • 举报
回复
Format('%s-%s-%s', [Copy('19780230', 1, 4), Copy('19780230', 5, 2), Copy('19780230', 7, 2)]);
王集鹄 2000-12-08
  • 打赏
  • 举报
回复
另一个函数FormatDateTime('YYYY''-''MM''-''DD', Now)
WorldCreater 2000-12-08
  • 打赏
  • 举报
回复
诸位多多援手
说清楚后,我再加点分
comanche 2000-12-08
  • 打赏
  • 举报
回复
这……
这不是能用 Format能解决的事
WorldCreater 2000-12-08
  • 打赏
  • 举报
回复
我还是不很清楚

比如:AnsiString Test = "123456789"
现在我想调用 Test.Format(param1,param2,param3)这样的一个方法
使得格式化后 Test == "123-456-789"
该怎么填入那三个参数
WorldCreater 2000-12-07
  • 打赏
  • 举报
回复
直接告诉我各个参数的含义吧

再给个例子,谢谢!
WorldCreater 2000-12-07
  • 打赏
  • 举报
回复
比如说:
  我想将字串"19780230"格式化为"1978-02-30"这样的
日期格式应怎么做
comanche 2000-12-07
  • 打赏
  • 举报
回复
哇!!干嘛那么可怕
跟 printf还不是一样
王集鹄 2000-12-06
  • 打赏
  • 举报
回复
Format('%%', []); //%
Format('%8s', ['1234']); // 1234
王集鹄 2000-12-06
  • 打赏
  • 举报
回复
//看看help
Format strings specify required formats to formatting routines.

Category

string formatting routines

Description

Format strings passed to the string formatting routines contain two types of objects -- literal characters and format specifiers. Literal characters are copied verbatim to the resulting string. Format specifiers fetch arguments from the argument list and apply formatting to them.
Format specifiers have the following form:

"%" [index ":"] ["-"] [width] ["." prec] type

A format specifier begins with a % character. After the % come the following, in this order:

An optional argument index specifier, [index ":"]
An optional left justification indicator, ["-"]
An optional width specifier, [width]
An optional precision specifier, ["." prec]
The conversion type character, type

The following table summarizes the possible values for type:

d Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros.
u Unsigned decimal. Similar to 'd' but no sign is output.
e Scientific. The argument must be a floating-point value. The value is converted to a string of the form "-d.ddd...E+ddd". The resulting string starts with a minus sign if the number is negative. One digit always precedes the decimal point.The total number of digits in the resulting string (including the one before the decimal point) is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present. The "E" exponent character in the resulting string is always followed by a plus or minus sign and at least three digits.
f Fixed. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative.The number of digits after the decimal point is given by the precision specifier in the format string梐 default of 2 decimal digits is assumed if no precision specifier is present.
g General. The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present.Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format.
n Number. The argument must be a floating-point value. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format corresponds to the "f" format, except that the resulting string contains thousand separators.
m Money. The argument must be a floating-point value. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator, and CurrencyDecimals global variables, all of which are initialized from the Currency Format in the International section of the Windows Control Panel. If the format string contains a precision specifier, it overrides the value given by the CurrencyDecimals global variable.
p Pointer. The argument must be a pointer value. The value is converted to an 8 character string that represents the pointers value in hexadecimal.
s String. The argument must be a character, a string, or a PChar value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated.
x Hexadecimal. The argument must be an integer value. The value is converted to a string of hexadecimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has fewer digits, the resulting string is left-padded with zeros.
Conversion characters may be specified in uppercase as well as in lowercase梑oth produce the same results.
For all floating-point formats, the actual characters used as decimal and thousand separators are obtained from the DecimalSeparator and ThousandSeparator global variables.
Index, width, and precision specifiers can be specified directly using decimal digit string (for example "%10d"), or indirectly using an asterisk character (for example "%*.*f"). When using an asterisk, the next argument in the argument list (which must be an integer value) becomes the value that is actually used. For example,

Format('%*.*f', [8, 2, 123.456])

is the same as

Format('%8.2f', [123.456]).


A width specifier sets the minimum field width for a conversion. If the resulting string is shorter than the minimum field width, it is padded with blanks to increase the field width. The default is to right-justify the result by adding blanks in front of the value, but if the format specifier contains a left-justification indicator (a "-" character preceding the width specifier), the result is left-justified by adding blanks after the value.

An index specifier sets the current argument list index to the specified value. The index of the first argument in the argument list is 0. Using index specifiers, it is possible to format the same argument multiple times. For example "Format('%d %d %0:d %1:d', [10, 20])" produces the string '10 20 10 20'.

The format strings are used by the following routines:

Format function
FormatBuf function
FmtStr procedure
StrFmt function
StrLFmt function

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧