1. Home
  2. Computing & Technology
  3. Delphi Programming

Path shortener: c:\AB\C...DE\F.ghi

By , About.com Guide

Sometimes when a huge path [any long string] is to be displayed in a small space, it is desirable to see the start and the end of the path with ellipses in-between, rather than truncating one of the ends.

For example
"C:\Program Files\Delphi\DDrop\TargetDemo\main.pas"
is desired to be seen as
"C:\Program F....Demo\main.pas"
then the following "Mince" function could be used.

~~~~~~~~~~~~~~~~~~~~~~~~~
function Mince
    (PathToMince :String; InSpace :Integer): String;
var TotalLength, FLength : Integer;
begin
  TotalLength := Length(PathToMince) ;
  if TotalLength > InSpace then
  begin
   FLength := (Inspace Div 2) - 2;
   Result := Copy(PathToMince, 0, fLength)
             + '...'
             + Copy(PathToMince,
                   TotalLength-fLength,
                   TotalLength) ;
  end
  else
    Result := PathToMince;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
The "InSpace" is an approximate number of charecters the intended space can hold. For example, if i need to fit a long path in a TLabel that can hold 10 charecters 'M' then InSpace is 10. Though it is approximate it does the job 99% of the time. Of course this could be modified to take into account the font width.

For example:
Label1.caption := Mince(Label1.caption, 15) ;

Note: Delphi's MinimizeName function returns a shortened version of a filename (using dots for folders) that fits into some "pixel" length.

Delphi tips navigator:
» The state of the Shift, Ctrl, Alt keys
« Determine the actual size of a Blob field in a Table

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Path shortener: c:\AB\C...DE\F.ghi

©2009 About.com, a part of The New York Times Company.

All rights reserved.