[Pascal] Any Ideas

Discussion in 'Mixed Languages' started by splinter_, Mar 8, 2013.

  1. splinter_

    splinter_ MDL Novice

    Dec 17, 2012
    19
    3
    0
    Hey guys. I want to create program that when I input 3 words as one string separated with "-" I want to print out third word then second the first.
    Example:
    If I input My-Digital-Life I want to print out Life-Digital-My

    If anyone have any ideas please answer!
    Thank you! :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,558
    3,779
    120
    #2 Michaela Joy, Mar 8, 2013
    Last edited by a moderator: Apr 20, 2017
    Hi Splinter,

    Are you using Delphi? If so, you can use the copy function to split the strings, and store them in a TStringList. Then create another TStringList and copy the strings from the end to the begining.

    Here's a few snippets of code that I wrote in Delphi 7. If you're using an Embarcadero product, you'll have to change things around so that the code will compile correctly.

    Code:
    unit WhatEverrr; // I expect you to take the code from here and move it into your own project. :)
    
    uses
      Windows, Sysutils;
    
      function SafeTrim(const S: String): String;
      procedure ChopStringAt(AString: String;AIndex,AGap: Integer;var lVal,rVal: String);
      procedure ChopString(ADelim,AString: String;var lVal,rVal: String);
      procedure SplitString(ADelim,AString: String;var lVal,rVal: String);
      procedure SplitStringToList(const ADelim,AString: String;slResult: TStrings);
    
    implementation
    
    uses
      Strutils;
    
    function SafeTrim(const S: String): String;
    var
      I,L: Integer;
    begin
    Result := '';
    if Length(S) <> 0 then
    begin
     L := 0;
     for I := 1 to Length(S) do
     begin
      if (S <> ' ') and (S <> #9) then
      begin
       L := I;
       Break;
      end;
     end;
     if L = 0 then
      Result := ''
     else
      Result := Copy(S,L,Length(S) - L + 1);
    end;
    end;
    
    procedure ChopStringAt(AString: String;AIndex,AGap: Integer;var lVal,rVal: String);
    begin
    if Length(AString) = 0 then
     Exit;
    lVal := Copy(AString,1,AIndex - 1);
    rVal := Copy(AString,AIndex + AGap,Length(AString) - AGap - (AIndex - 1));
    end;
    
    procedure ChopString(ADelim,AString: String;var lVal,rVal: String);
    var
      P: Integer;
    begin
    P := Pos(ADelim,AString);
    if P <> 0 then
     ChopStringAt(AString,P,Length(ADelim),lVal,rVal)
    else
    begin
     lVal := AString;
     rVal := '';
    end;
    end;
    
    procedure SplitString(ADelim,AString: String;var lVal,rVal: String);
    begin
    ChopString(ADelim,AString,lVal,rVal);
    lVal := SafeTrim(lVal);
    rVal := SafeTrim(rVal);
    end;
    
    procedure SplitStringToList(const ADelim,AString: String;slResult: TStrings);
    var
      lVal,rVal,S1: String;
    begin
    if not Assigned(slResult) then
     Exit;
    S1 := AString;
    while Pos(aDelim,S1) <> 0 do
    begin
     SplitString(ADelim,S1,lVal,rVal);
     if Length(lVal) <> 0 then
      slResult.Add(lVal);
     S1 := rVal;
    end;
    if Length(S1) <> 0 then
     slResult.Add(S1);
    end;
    
    end.
    



    So, you'd write something like this...
    Code:
    var
      sl1: TStringList;
      I: Integer;
    
     sl1 := TStringList.create;
     sl1.sorted := False;
     sl1.clear;
     slResult.clear;
     try
        SplitStringToList('-',MyString,sl1);
      if MyString.count > 1 then
      begin
      for I := sl1.count - 1 downto 0 do
       slResult.add(sl1);
    
      end
      else
       slResult.add(sl1[0]);
     finally
      sl1.free;
     end;
    
    

    SplitString removes any leading and trailing spaces from the string being split; ChopString does not.

    So, if you need to keep trailing spaces, substitute ChopString() for SplitString() in SplitStringToList().
    Or better yet, just create another list function called ChopStringToList()

    Bear in mind that both ChopString and SplitString are recursive, which makes them frustrating to look at in a debugger.

    I hope this works for you.

    :Miki.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. splinter_

    splinter_ MDL Novice

    Dec 17, 2012
    19
    3
    0
    Thank you Michaela it's great :) !!!
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...