Rida Shamasneh
Rida Shamasneh

Reputation: 826

How to check if a string ends with another (EndsWith) in Inno Setup

I need to write a logic in some Inno Setup function that checks if a string ends with another.

Can I use StrUtils Pascal functions (EndsWith) to do so?

function NextButtonClick(CurPageID: Integer): Boolean;
var
  dir_value: String; app_name: String; 
begin
  if CurPageID = wpSelectDir then
    begin
      dir_value := "C:\work\ABC"
      app_name :=  "ABC"

      { I need to write a logic here to check if dir_value ends with app_name }
    end;
end;

Upvotes: 2

Views: 937

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202474

There's no EndsWith in Inno Setup.

But you can easily implement it:

function EndsWith(SubText, Text: string): Boolean;
var
  EndStr: string;
begin
  EndStr := Copy(Text, Length(Text) - Length(SubText) + 1, Length(SubText));
  // Use SameStr, if you need a case-sensitive comparison
  Result := SameText(SubText, EndStr);
end;

Though in your case, you actually need something like this:

function EndsWithFileName(FileName, Path: string): Boolean;
begin
  Result := SameText(FileName, ExtractFileName(Path));
end;

For SameText (and SameStr), you need Inno Setup 6. On older versions, you can replace them with CompareText (and CompareStr).

Upvotes: 2

Related Questions