Jump to content

Recommended Posts

I wish everyone a healthy day.

I have a DIMENSION data as a string  like  '10*20*30'

I want to get each dimension number

if user doesnt put empty string between the numbers like(10*20*30) there is no problem

but if user put empty string between the numbers like (10 * 20 * 30) then i cant get correct result

 

procedure TForm1.Button1Click(Sender: TObject);
  var
  i: Integer;
  Str, Seperator : String;
begin
  Edit1.Text := '10 * 20 * 30';
  Str := Trim(Edit1.Text);  // i hoped that Trim removed the empty strings that is converted the Str to 10*20*30 but no
  for i := 1 to Length(Str) do
  if Str in ['*'] then
  begin
    Seperator := Str;
    Break;
  end;

  Edit2.Text := SplitString(Str,Seperator)[0];
  Edit3.Text := SplitString(Str,Seperator)[1];
  Edit3.Text := SplitString(Str,Seperator)[2];
end;

 

Thank You

 

Share this post


Link to post
Posted (edited)

var

 numbers : TArray<string>;

begin

 numbers :=  '10 * 20 * 30'.Replace(' ', '', [rfReplaceAll]).Split(['*'], TStringSplitOptions.ExcludeEmpty]);

 

Trim removes only the starting or ending whitespace.


 

 

Edited by mvanrijnen
  • Like 1

Share this post


Link to post
 procedure TForm1.Button1Click(Sender: TObject);
  var
  i: Integer;
  Str, Seperator : String;
  LArr : TArray<String>;
begin
  Edit1.Text := '10 * 20 * 30';
  LArr := Edit1.Text.Split( [ '*' ] );

  if Length( LArr ) < 3 then
  begin
     // Error;
     Exit;
  end;
  
  Edit2.Text := LArr[ 0 ].Trim;
  Edit3.Text := LArr[ 1 ].Trim;
  Edit3.Text := LArr[ 2 ].Trim;
end; 

 

Share this post


Link to post
Posted (edited)

declaration:

type
  TStringArray = TArray<string>;
  TIntegerArray = TArray<integer>;

  TStringArrayHelper = record helper for TStringArray
    function ToIntArray() : TIntegerArray;
  end;

  TIntegerArrayHelper = record helper for TIntegerArray
    class function FromString(const AValue : string; const ASeparator : Char = ';') : TIntegerArray;
  end;

 

 

implementation:

{ TStringArrayHelper }

function TStringArrayHelper.ToIntArray: TIntegerArray;
var
  idx : integer;
begin
  SetLength(Result, Length(Self));
  for idx := Low(Self) to High(Self) do
      Result[idx] := Self[idx].ToInteger;
end;


{ TIntegerArrayHelper }

class function TIntegerArrayHelper.FromString(const AValue: string; const ASeparator: Char): TIntegerArray;
begin
  Result := AValue.Replace(' ', '', [rfReplaceAll]).Split([ASeparator], TStringSplitOptions.ExcludeEmpty).ToIntArray;
end;

 

 

usecase:

procedure TForm1.Button4Click(Sender: TObject);
const
  CNST_TEST_Value = '10 *   20 *    30';
var
  lValues : TIntegerArray;
begin
  lvalues :=  TIntegerArray.FromString(CNST_TEST_Value, '*');
end;

 

🙂 

 

Edited by mvanrijnen

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×