달 이름을 숫자 1-12로 변환하는 내장 델파이 (XE2)/Windows 메소드가 있습니까? (TFormatSettings.)LongMonthNames[]
자신을 통해 반복하는 대신?델파이에서 월 이름을 숫자로 변환 하시겠습니까?
답변
IndexStr
에서 StrUtils
까지 사용할 수 있습니다. 문자열이없는 경우 -1
을 반환합니다.
Caption := IntToStr(
IndexStr(FormatSettings.LongMonthNames[7], FormatSettings.LongMonthNames) + 1);
편집 :
Function GetMonthNumber(Const Month:String):Integer; overload;
begin
Result := IndexText(Month,FormatSettings.LongMonthNames)+1
end;
(또는 ANSIText이 경우에 없다 꽤 확신과를 formatsetting 시스템과 기능 무감각 함). 나는이 방법을 사용하지 않을 것이다. 왜냐하면 String을 던져야하기 때문이다. <-> ANSIString과 나의 코드는 모든 괄호로 읽을 수 없게된다 ;-) 나는이 답안을 수여 할 것이지만 'the'정답으로 표시하지 않을 것이다. 그리고 노력에 감사드립니다. –
내가 방법을 찾을 수없는하지만 난 하나를 쓰기 :
당신이 같이 IndexText
을 사용할 수 있습니다 주조 및 사례 감도 문제를 방지하려면. ;-)
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;
전화
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) : 정수입니다. 초과 적재;
나는 표준이에 대한 기능 ... – Whiler