Recent

Author Topic: [SOLVED]Find Nth occurrence of number data in string  (Read 3005 times)

Arctic_Eddie

  • Jr. Member
  • **
  • Posts: 93
[SOLVED]Find Nth occurrence of number data in string
« on: February 05, 2015, 06:34:20 pm »
I need to parse a line of text which has several groups of numerical data. I need the text from the Nth group so I can convert it to Real with the Val() function. I've started a function:
 
function TForm1.FindNthWord( aline :String; acol :integer ) :String;

where 'aline' is a complete line of text and 'acol' is the Nth group of text separated by spaces
returns the text in the Nth group

For example, here is a partial line of text:
   90.00     0.00    -999.99   12.44   12.44    0.00000   -90.00

I want to return the 4th group, 12.44. The number of spaces and size of the text clusters is not constant. I can think of a very messy way of doing this but I need something more elegant.

Can some one point me to a string function that will help? I've only found PosEx so far.


« Last Edit: February 05, 2015, 07:20:48 pm by Arctic_Eddie »

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Find Nth occurrence of number data in string
« Reply #1 on: February 05, 2015, 06:48:48 pm »
just use a tstringlist with seperator the space
add your string
remove blanks if any and you r done
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1931
Re: Find Nth occurrence of number data in string
« Reply #2 on: February 05, 2015, 06:49:51 pm »
Code: [Select]
var SL:TStringList;
begin
 SL:=TStringList.Create;
 SL.DelimitedText:='90.00     0.00    -999.99   12.44   12.44    0.00000   -90.00';
 ShowMessage(SL[4-1]);
 SL.Free;
end; 

Arctic_Eddie

  • Jr. Member
  • **
  • Posts: 93
Re: Find Nth occurrence of number data in string
« Reply #3 on: February 05, 2015, 07:18:54 pm »
Now that's elegant, simple, and it works fine for my data.

Thank you so much.

 

TinyPortal © 2005-2018