George Hovhannisian
George Hovhannisian

Reputation: 707

inno setup - Delete arbitrary substring from a string

I need to delete a substring which is arbitrary each time. For example: ..\HDTP\System\*.u should become ..\System\*.u and/or ..\New Vision\Textures\*.utx should become ..\Textures\*.utx. More specifically: Ignore the first three characters, delete whatever comes after, until the next \ character (including that character), leave the rest of the string intact. Could you, please, help me with this? I know, I have the worst explaining skills in the whole world, if something isn't clear, I'll try to explain again.

Upvotes: 3

Views: 1941

Answers (1)

Bongo
Bongo

Reputation: 3153

This is a little bit of copy and split work for Inno Setup but I have here a function for you with some extra comments. Read it carefully as it isn't tested properly and if you have to edit it you will have to know what it is doing ;)

function FormatPathString(str : String) : String;
var
    firstThreeChars       : String;
    charsAfterFirstThree  : String;
    tempString  : String;
    finalString  : String;
    dividerPosition   : Integer;
begin

  firstThreeChars := Copy(str, 0, 3);                //First copy the first thee character which we want to keep
  charsAfterFirstThree := Copy(str,4,Length(str));   //copy the rest of the string into a new variable
  dividerPosition := Pos('\', charsAfterFirstThree); //find the position of the following '\'
  tempString := Copy(charsAfterFirstThree,dividerPosition+1,Length(charsAfterFirstThree)-dividerPosition);            //Take everything after the position of '\' (dividerPosition+1) and copy it into a temporary string
  finalString := firstThreeChars+tempString;         //put your first three characters and your temporary string together
  Result := finalString;                             //return your final string
end; 

And this is how you would call it

FormatPathString('..\New Vision\Textures\*.utx');

You will have to rename the function and the var's so that it will match your program but I think this will help you.

Upvotes: 4

Related Questions