2012-12-04 3 views

답변

7

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; 
+0

(또는 ANSIText이 경우에 없다 꽤 확신과를 formatsetting 시스템과 기능 무감각 함). 나는이 방법을 사용하지 않을 것이다. 왜냐하면 String을 던져야하기 때문이다. <-> ANSIString과 나의 코드는 모든 괄호로 읽을 수 없게된다 ;-) 나는이 답안을 수여 할 것이지만 'the'정답으로 표시하지 않을 것이다. 그리고 노력에 감사드립니다. –

0

내가 방법을 찾을 수없는하지만 난 하나를 쓰기 :
당신이 같이 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; 
+1

"="대신 SameText를 사용하여 대소 문자를 구분하지 않고 비교할 수 있습니다. 또한 TFormatSettings 매개 변수를 사용하는이 함수의 오버로드 된 버전은 무엇입니까? – dummzeuch

+0

@dummzeuch : 함수 오버로드에 대해 이해하지 못합니까? – Ravaut123

+0

function GetMonthNumberofName (AMonth : String) : 정수입니다. 초과 적재; 함수 GetMonthNumberofName (AMonth : String, AFormatSettings : TFormatSettings) : 정수입니다. 초과 적재; 두 번째 변형은 System.SysUtils.FormatSettings 대신 지정된 AFormatSettings 매개 변수를 사용하여 프로그램에서 운영 체제에 구성된 것과 다른 로캘로 변환해야하는 경우를 처리합니다. 죄송합니다 , 줄 바꿈을 허용하지 않습니다. – dummzeuch