Reputation: 141
I have a path say C:\Program Files\Borland what would bet the easiest way to parse that string and just return Borland? thanks
Upvotes: 14
Views: 16528
Reputation: 21
This will work on all folders whether it has a trailing back slash or not:
ExtractFileName(ExcludeTrailingBackslash(Path))
Upvotes: 2
Reputation: 20132
To directly parse that string and just return "Borland", you can do this:
uses SysUtils;
Delete(Path, 1, LastDelimiter('\', Path));
Upvotes: 1
Reputation: 136391
try using the ExtractFileName function, this function only works (for your example) if your path not finalize with an backslash, so you can use the ExcludeTrailingPathDelimiter function to remove the final backslash.
see this sample
program ProjectExtractPathDemo;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
Path : string ;
begin
Path:='C:\Program Files\Borland';
Writeln(ExtractFileName(Path));//return Borland
Path:='C:\Program Files\Borland\';
Writeln(ExtractFileName(Path));//return ''
Path:='C:\Program Files\Borland\';
Writeln(ExtractFileName(ExcludeTrailingPathDelimiter(Path)));//return Borland
Readln;
end.
check this link for more info
Upvotes: 31
Reputation: 84540
You can get whatever comes after the last backslash with ExtractFileName
, which is found in the SysUtils unit.
Upvotes: 8