是否有一些内置的Delphi(XE2)/ Windows方法将月份名称转换为数字1-12;而不是自己通过(TFormatSettings.)LongMonthNames[]
循环?在Delphi中将月份名称转换为数字?
回答
您可以使用IndexStr
从StrUtils
返回-1
如果未找到字符串例如
Caption := IntToStr(
IndexStr(FormatSettings.LongMonthNames[7], FormatSettings.LongMonthNames) + 1);
编辑:
为了避免与铸造和大小写问题,你可以使用IndexText
如图所示:
Function GetMonthNumber(Const Month:String):Integer; overload;
begin
Result := IndexText(Month,FormatSettings.LongMonthNames)+1
end;
(或者ANSIText的情况下,函数不敏感)。我不会这样做,因为那时我必须强制使用字符串<-> ANSIString,并且我的代码在所有括号中都变得难以理解;-)我将授予这个答案,但不会将其标记为'正确答案。并感谢您的努力。 –
我不能找到一种方法,但我写一个。 ;-)
function GetMonthNumberofName(AMonth: String): Integer;
var
intLoop: Integer;
begin
Result:= -1;
if (not AMonth.IsEmpty) then
begin
for intLoop := Low(System.SysUtils.FormatSettings.LongMonthNames) to High(System.SysUtils.FormatSettings.LongMonthNames) do
begin
//if System.SysUtils.FormatSettings.LongMonthNames[intLoop]=AMonth then --> see comment about Case insensitive
if SameText(System.SysUtils.FormatSettings.LongMonthNames[intLoop], AMonth) then
begin
Result:= Intloop;
Exit
end;
end;
end;
end;
好吧,我改变这个功能为其他FormatSettings。
function GetMonthNumberofName(AMonth: String): Integer; overload;
function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer; overload;
function GetMonthNumberofName(AMonth: String): Integer;
begin
Result:= GetMonthNumberofName(AMonth, System.SysUtils.FormatSettings);
end;
function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer;
var
intLoop: Integer;
begin
Result:= -1;
if (not AMonth.IsEmpty) then
begin
for intLoop := Low(AFormatSettings.LongMonthNames) to High(AFormatSettings.LongMonthNames) do
begin
if SameText(AFormatSettings.LongMonthNames[intLoop], AMonth) then
begin
Result:= Intloop;
Exit
end;
end;
end;
end;
与电话系统formatsetting
GetMonthNumberofName('may');
或FormatSetting
procedure TForm1.Button4Click(Sender: TObject);
var
iMonth: Integer;
oSettings:TFormatSettings;
begin
// Ned
// oSettings:= TFormatSettings.Create(2067);
// Fr
// oSettings:= TFormatSettings.Create(1036);
// Eng
oSettings:= TFormatSettings.Create(2057);
iMonth:= GetMonthNumberofName(self.Edit1.Text, oSettings);
showmessage(IntToStr(iMonth));
end;
我可能会做一个不区分大小写的比较,使用SameText而不是“=”。另外,这个函数的重载版本需要TFormatSettings参数呢? – dummzeuch
@dummzeuch:我不明白超载的功能? – Ravaut123
function GetMonthNumberofName(AMonth:String):Integer;超载;
- 1. 将月份名称转换为数字
- 2. 如何将月份数字转换为月份短名称?
- 3. 转换月份名称为月数
- 4. 在python中将名称月份转换为数字
- 5. 如何在Oracle中将月份数字转换为完整的月份名称?
- 6. 如何将数字月份数字转换为使用PHP的月份名称
- 7. 将月份名称转换为SQL Server中的月份编号
- 8. 如何将月份名称转换为月份在Ruby中整数?
- 9. 如何将月份从unix时间戳从数字转换为月份名称?
- 10. 如何将月份名称转换为数字?
- 11. 使用sql将月份编号转换为月份名称
- 12. 将月份名称转换为实际月份
- 13. 将月份int转换为月份名称
- 14. 使用JavaScript将月份名称转换为月份编号
- 15. 将月份编号转换为月份名称
- 16. 将缩短的月份名称转换为较长的月份名称
- 17. 如何月份名称数组转换为数字阵列
- 18. 使用Carbon将月份名称转换为字符串编号?
- 19. 如何将特定的字符串转换为月份名称
- 20. 用于在perl中将数字转换为月份名称的模块
- 21. PHP - 将月份数量转换为月份短名称,而我从Mysqli调用
- 22. 将月份mmm转换为数字
- 23. 哈希将月份转换为数字
- 24. 如何将数字月份转换为C#控制台中的月份全名
- 25. 将某一年份的周编号转换为月份名称
- 26. 如何操作哈希键数字月份转换为名称
- 27. 如何将月份号码转换为python中的月份名称
- 28. 简单的方法将月份名称转换为月份编号在php
- 29. 将文本框中的数字转换为vba语句的月份名称
- 30. 在Oracle中将希伯来语月份名称转换为英文月份名称
我肯定有这个没有标准的功能... – Whiler