是否有一些內置的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