As I could not find a function into the RTL to do that, here is a function to cut too long strings. The advantage is to have the right part of the string in the same time. It is usefull to me to display long paths. I don't know if it will be usefull to you but I share it, just in case.


program piko;
{$mode objfpc}

uses sysUtils;

function CutString(const s: UTF8String; const maxLen: integer = 30; const rightSide: boolean = false; const rightSideLen: integer = 8) : UTF8String;
var l, i : integer;
begin
    l := length(s);

    if (0 = maxLen) or (l < maxLen) or (rightSideLen > maxLen - 4) then
    begin
        result := s;
        exit;
    end;

    if rightSide then
    begin
        i := l - (rightSideLen + 3);
result := TrimRight(leftStr(s, i)) + '...' + TrimLeft(rightStr(s, rightSideLen));
    end else begin
        result := leftStr(s, maxLen - 3) + '...';
    end;
end;

const TEXT_TO_REDUCE = 'here is a very long text and it would nice to cut it !';
begin
writeln(CutString(TEXT_TO_REDUCE)); // here is a very long text an... writeln(CutString(TEXT_TO_REDUCE, 30, true)); // here is a very long text and it would nice...cut it !
end.



--
Damien Gerard
[EMAIL PROTECTED]



_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to