Henry Olive 5 Posted July 21, 2021 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
mvanrijnen 123 Posted July 21, 2021 (edited) var numbers : TArray<string>; begin numbers := '10 * 20 * 30'.Replace(' ', '', [rfReplaceAll]).Split(['*'], TStringSplitOptions.ExcludeEmpty]); Trim removes only the starting or ending whitespace. Edited July 21, 2021 by mvanrijnen 1 Share this post Link to post
Rollo62 536 Posted July 21, 2021 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
mvanrijnen 123 Posted July 21, 2021 (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 July 21, 2021 by mvanrijnen Share this post Link to post
Henry Olive 5 Posted July 22, 2021 Thank you so much Rollo62, Mvanrijnen Share this post Link to post