How to split a text and get value from it? lazarus / 61-Beginners ---------------------------------------------------------------------- Doe · 2021-01-30 13:42 Hello guys, I was trying to split a text such as "Initial velocity = 10", and assign 10 into a variable. I want to modify it so if input "Initiavelocity=10" or "initial velocity=10" or even "IV=10". It can perform the same function. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit InputUVAforT; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, strutils, StdCtrls; type   { TInputUVAforT }   TInputUVAforT = class(TForm)    Button1: TButton;    Label1: TLabel;    Values: TMemo;    procedure Button1Click(Sender: TObject);  private   public   end; var  FInputUVAforT: TInputUVAforT;  i:integer;  A: TStringArray;  toxt,Vr: string;  InatialV,FinalV,Acceleration:real; implementation {$R *.lfm} { TInputUVAforT } procedure TInputUVAforT.Button1Click(Sender: TObject);begin  for i:=0 to Values.lines.count-1 do      begin         toxt:=Values.lines[i];         toxt:=DelSpace(toxt);         A:= toxt.Split('=');         if (A[0]='Initialcelocity') then         begin         InatialV:=StrToFloat(A[1]);         end         else if A[0]='Finalvelocity' then         begin         FinalV:=StrToFloat(A[1]);         end         else if A[0]='Acceleration' then         begin         Acceleration:=StrToFloat(A[1]);         end         end;      showmessage(FloatToStr(InatialV));      showmessage(FloatToStr(FinalV));      showmessage(FloatToStr(Acceleration));end; end. ---------------------------------------------------------------------- howardpc · 2021-01-30 14:25 Mode objfpc won't let you reuse text since it is already pre-declared. Use a local variable, something like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm2.Button1Click(Sender: TObject);var  txt: String;  i: Integer;begin  for i:=0 to Values.lines.count-1 do    begin      txt := DelSpace(Values.Lines[i]);      A := txt.Split('=');      if Length(A) > 1 then        case A[0] of          'Initialvelocity': InitialV := StrToFloat(A[1]);          'Finalvelocity':   FinalV := StrToFloat(A[1]);          'Acceleration':    Acceleration := StrToFloat(A[1]);        end;    end;  showmessage(FloatToStr(InitialV));  showmessage(FloatToStr(FinalV));  showmessage(FloatToStr(Acceleration));end; ---------------------------------------------------------------------- wp · 2021-01-30 14:38 Values is a TMemo, and thus its Lines is a TStrings descendant. This class already offers splitting name/value pairs: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  initialV := StrToFloat(Memo1.Lines.Values['InitialVelocity']);  finalV := StrToFloat(Memo1.Lines.Values['FinalVelocity']);  acceleration := StrToFloat(Memo1.Lines.Values['Acceleration']);   ShowMessage(Format(    'Initial velocity: %f' + LineEnding +    'Final velocity: %f' + LineEnding +    'Acceleration: %f',    [initialV, finalV, acceleration])  );end; ---------------------------------------------------------------------- Doe · 2021-01-30 14:48 @wp I tried your one and it works. Thanks. But if I input"Initial Velocity=10". The program is crushed. How to modify it? ---------------------------------------------------------------------- wp · 2021-01-30 16:03 Why should this be possible? Do you allow users to enter name/value pairs in a memo? A bad idea because there are so many ways to misspell words, add spaces etc etc. Create a form with edit controls (or floatspinedit(ex) controls for numerical input) in which the users can enter these values and then you can write your data files in a standardized way. Anyway, to some degree it is possible to catch name errors. Write a function which accepts all possible spellings that you allow and search each one in the Memo.Lines. Use the TStrings.Names[ i] property to split off the name part for the line at index i. Convert everything to lowercase before comparing to avoid UPPERCASE/lowercase/CamelCase issues. Use the trim function to remove spaces added around the '=' -- another annoyance of the TStrings name/value handling. And you can also use the SoundEx function of the unit StrUtils to allow for "similar" spellings: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  ERROR_CODE = -999; function GetNameIndex(AList: TStrings; const ANames: array of string): Integer;var  name: String;  i: Integer;  name_SoundEx: String;begin  for name in ANames do    for i:=0 to AList.Count-1 do      if Lowercase(trim(AList.Names[i])) = Lowercase(name) then      begin        Result := i;        exit;      end;   for name in ANames do  begin    name_SoundEx := SoundEx(trim(lowercase(name)));    for i := 0 to AList.Count-1 do    begin      if SoundEx(trim(lowercase(AList.Names[i]))) = name_SoundEx then      begin        Result := i;        exit;      end;    end;  end;   Result := -1;end;   procedure TForm1.Button1Click(Sender: TObject);var  initialV: Double = ERROR_CODE;  finalV: Double = ERROR_CODE;  i: Integer;  v: string;begin  i := GetNameIndex(Memo1.Lines, ['InitialVelocity', 'Initial Velocity', 'IV']);  if i > -1 then begin    v := trim(Memo1.Lines.ValuefromIndex[i]);    TryStrToFloat(v, initialV);  end;   i := GetNameIndex(Memo1.Lines, ['FV', 'FinalVelocity', 'Final Velocity', 'end velocity']);  if i > -1 then  begin    v := trim(Memo1.Lines.ValuefromIndex[i]);    TryStrToFloat(v, finalV);  end;   ShowMessage(Format('Initial velocity: %f'#13'Final velocity: %f', [initialV, finalV]));end; ---------------------------------------------------------------------- Bart · 2021-01-30 16:12 Use a TValueListEditor if you want to display Key-Value pairs. You can forbid the editing of keys (in the ObjectInspector). Bart ---------------------------------------------------------------------- cdbc · 2021-01-31 15:36 Hi I use this to pick the fields in a string: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { usage: Surname:= GetFieldToken(2,'Benny|Christensen','|') => 'Christensen'  or }{        Firstname:= GetFieldToken(1,'Benny Christensen',' ') => 'Benny' }function Getfieldtoken(const Fieldno: integer;const S: string;const Separator: char): string;var  B,E,C,I,Len: integer;  InField: boolean;begin  Len:= system.length(S);  if (Len > 0) and (FieldNo > 0) then begin                 { save clockcycles }    I:= 0; C:= 1; InField:= false; B:= -1; E:= -1;            { initialization }    while (C <= Len) do begin      if (system.copy(S,C,1) = Separator) or         (C = Len) then inc(I);        { check for separator and end of string }      if (I = FieldNo-1) and not InField then begin        { 0-based by nature }        B:= C;                           { point b to beginning of substring }        InField:= true;               { flag field found, now look for the end }      end;      if (I = FieldNo) and InField then begin        E:= C;                                 { point e to end of substring }        break;                                  { Field found, we're done here }      end;      inc(C);                                             { increment cursor }    end;                                                        { continue ? }    if (B <> -1) and (E <> -1) then begin      if E = Len then Result:= system.copy(S,B+1,E-B)       { special cases at }      else if B = 1 then Result:= system.copy(S,B,E-B)     { beginning and end }      else Result:= system.copy(S,B+1,E-B-1);    end else Result:= '';                     { return empty string on error }  end else Result:= '';           { if fed an empty string return it untouched }end; { getfieldtoken } and use it like this: res:= strtoint(GetFieldToken(2,'IV=10','='));  perhaps it can be of use to you Regards Benny ---------------------------------------------------------------------- ArtLogi · 2021-02-01 16:15 There is also useful function ExtractWord StrUtils. https://www.freepascal.org/docs-html/rtl/strutils/extractword.html Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Program extractword;Uses SysUtils, strutils ;var   SplitChar: TSysCharSet; // word delimiter character for ExtractWord function  StringVariable: String;  nThWord: integer; Begin SplitChar:=[#61]; // IIRC... #61 is  code for equal signnThWord:= 1;  // index begins from 0 so zero is left side and 1 is right side and and so on. (* DelSpace removes white spaces from the extracted word '0 . 0 1' *)StringVariable:= DelSpace(Extractword(nThWord, 'TheNumber = 1 . 0 1', SplitChar)); writeln(StringVariable); // Show string at console at console program, replace with Showmessage() in windowed app.readln;  // For console. Waits for user input. End.   ... but better to listen WP than me. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin(* Consider changing '.' to DecimalSeparator as srtToFloat conversion is now localised in freepascal and needs to have dot or comma depending your OS settings. The variable DecimalSeparator contains the right decimal separator for FPC and which is used at your OS localisation *)  if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;  // Now the edit field do not accept any other characters from keyboard input than those listed inside bracketsend;   Edit. Corrected typos and added correct unit for ExtractWord as StrUtils, removed Trim() and replaced with delspace() .... wrong language different meaning.