Results 1 to 5 of 5

Thread: fstring()

  1. #1
    Join Date
    Jul 2014
    Location
    My computer
    Posts
    78
    Mentioned
    8 Post(s)
    Quoted
    21 Post(s)

    Default fstring()

    Was trying to make a pretty proggy and needed something to format either side of a string so the ends would be even regardless of the info presented. I have no idea how to do this in lape or if you even can, so I made a function to format strings so they're either aligned to the left, right, or center. It fills the sides of the string with whatever character you choose depending on how you want it aligned.

    If that didn't make sense it's cause I'm terrible at explaining things , so I will let the code do the explaining.

    fstring():
    Simba Code:
    type
      alignType = (left, right, center);
    (*
      fstring()
      by: HKbotz

        Formats the string so it is either aligned to the left, right, or center.
        Fills sides based on align with whatever character you choose for specified
        length.

        -str: the string to format
        -len: the length of the formatted string
        -align: the way you wish to align the text compared to the fill char
        -fill: the character you wish to fill the sides with

        usage:
          fstring('Some text', 30);
          fstring('Some text', 30, center, '-');

        example:
          writeln('//  ' + fstring('SomeStat', 20) + '\\');
          writeln('//  ' + fstring('SomeOtherStat', 20) + '\\');
        Output:
          //  SomeStat            \\
          //  SomeOtherStat       \\
    *)

    function fstring(str: string; len: integer; align: alignType = left; fill: char = ' '): string;
    var
      i, c, n, strLen: integer;
    begin
      setLength(result, len);
      strLen := length(str);
      if (align = left) then
      begin
        for (i := 1) to (len) do
        begin
          if (i <= strLen) then
            result[i] := str[i]
          else
            result[i] := fill;
        end;
      end else if (align = right) then
      begin
        n := len - strLen;
        c := 1;
        for (i := 1) to (len) do
        begin
          if (i <= n) then
            result[i] := fill
          else
          begin
            result[i] := str[c];
            inc(c);
          end;
        end;
      end else if (align = center) then
      begin
        n := round((len - strLen) / 2);
        c := 1;
        for (i := 1) to (len) do
        begin
          if (i <= n) then
            result[i] := fill
          else if (c <= strLen) then
          begin
            result[i] := str[c];
            inc(c);
          end else
            result[i] := fill;
        end;
      end else
      begin
        writeln('Passed incorrect align param. Use right, left, or center.');
        writeln('Returning your original string...');
        setLength(result, strLen);
        result := str;
      end;
    end;


    Example:
    Simba Code:
    procedure exampleProggy();
    var
      logType: string = 'Willow';
    begin
      writeln('//' + fstring('', 30, , '-') + '\\');
      writeln('//' + fstring('Fake log chopping script', 30, center) + '\\');
      writeln('//' + fstring('', 30) + '\\');
      writeln('//' + fstring('  Log type: ' + logType, 30) + '\\');
      writeln('//' + fstring('  Logs chopped: ' + intToStr(230), 30) + '\\');
      writeln('//' + fstring('  Runs done: ' + intToStr(8), 30) + '\\');
      writeln('//' + fstring('  Time running: ' + timeRunning(TIME_SHORT), 30) + '\\');
      writeln('//' + fstring('', 30, , '-') + '\\');
    end;

    begin
      exampleProggy();
    end.
    Output from exampleProggy():
    Code:
    //------------------------------\\
    //   Fake log chopping script   \\
    //                              \\
    //  Log type: Willow            \\
    //  Logs chopped: 230           \\
    //  Runs done: 8                \\
    //  Time running: 02s           \\
    //------------------------------\\
    If the string you supply is longer than len, it will just return the first len charaters.
    So if you have a string that is 'This is a really long string', but only supply a len of 10, it will return 'This is a '.

    Hope that makes sense and that someone can get some use out of this(so far I'm just using for proggies).
    If there's a better way to do the formatting thing please let me know .

  2. #2
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Nice work! Also check out the Pad* functions that Simba provides.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  3. #3
    Join Date
    Jul 2014
    Location
    My computer
    Posts
    78
    Mentioned
    8 Post(s)
    Quoted
    21 Post(s)

    Default

    Quote Originally Posted by Daniel View Post
    Nice work! Also check out the Pad* functions that Simba provides.
    I knew there had to be something like that, just didn't know what lol. I still like to be able to pad with whatever character I want though, so I still prefer mine

  4. #4
    Join Date
    Nov 2014
    Posts
    104
    Mentioned
    12 Post(s)
    Quoted
    59 Post(s)

    Default

    Quote Originally Posted by HKbotz View Post
    I knew there had to be something like that, just didn't know what lol. I still like to be able to pad with whatever character I want though, so I still prefer mine
    You can use any character with the PadL / PadR functions.

    Simba Code:
    begin
      WriteLn(PadL('Test', 10, '~'));
    end.
    Code:
    ~~~~~~Test
    Successfully executed.
    Regardless, good work. Sometimes reinventing the wheel can teach you a lot.

  5. #5
    Join Date
    Jul 2014
    Location
    My computer
    Posts
    78
    Mentioned
    8 Post(s)
    Quoted
    21 Post(s)

    Default

    Quote Originally Posted by akarigar View Post
    You can use any character with the PadL / PadR functions.

    Simba Code:
    begin
      WriteLn(PadL('Test', 10, '~'));
    end.
    Code:
    ~~~~~~Test
    Successfully executed.
    Regardless, good work. Sometimes reinventing the wheel can teach you a lot.
    Well damn. Good to know for future reference. I guess my last attempt to justify mine is I can center text lol. I didn't notice a pad function that does that, however I won't be surprised if there is one lol.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •