Recent

Author Topic: TString problem(Solved)  (Read 1001 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TString problem(Solved)
« on: July 19, 2022, 08:26:49 am »
I have a tstring which I believe looks like the following:
   $000000000658E5D8^: '51,40,45,44,44,40,'

I can't figure a way to extract the substrings '51', '40', '45', and '44' the first 4 sets.
The number will change all the time from 1 to 52
Is there a way to do this.
  Thanks
« Last Edit: July 19, 2022, 05:16:33 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: TString problem
« Reply #1 on: July 19, 2022, 08:46:31 am »
TStringList.CommaText

alpine

  • Hero Member
  • *****
  • Posts: 1061
Re: TString problem
« Reply #2 on: July 19, 2022, 09:16:26 am »
One of the possibilities:
Code: Pascal  [Select][+][-]
  1. uses
  2.   StrUtils;
  3. var
  4.   S, T: String;
  5.   I, N: Integer;
  6. begin
  7.   S :=  '51,40,45,44,44,40,';
  8.   I := 1;
  9.   N := 1;
  10.   while (I <= Length(S)) and (N <= 4) do
  11.   begin
  12.     // Extract the N-th (N=1..4) comma sep. string
  13.     T := ExtractSubstr(S, I, [',']);
  14.     WriteLn(N, '=', T);
  15.     Inc(N);
  16.   end;
  17. end.
Why you don't use array of Integer instead?

Edit: Wait a minute, what is a tstring?
« Last Edit: July 19, 2022, 09:19:05 am by y.ivanov »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Ally

  • Jr. Member
  • **
  • Posts: 53
Re: TString problem
« Reply #3 on: July 19, 2022, 09:58:11 am »
Split is probably the easiest thing to do.

Code: Pascal  [Select][+][-]
  1. var
  2.   i: Integer;
  3.   Row: String;
  4.   Parts: TStringArray;
  5.   S1, S2, S3, S4: String;
  6. begin
  7.   Row :=  '51,40,45,44,44,40,';
  8.   Parts := Row.Split(',');
  9.   S1 := Parts[0];
  10.   S2 := Parts[1];
  11.   S3 := Parts[2];
  12.   S4 := Parts[3];
  13. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: TString problem
« Reply #4 on: July 19, 2022, 10:55:56 am »
Yup, that's how I would do it. Good little example.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: TString problem
« Reply #5 on: July 19, 2022, 01:06:09 pm »
There is also ExtractDelimited function from StrUtils.
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

alpine

  • Hero Member
  • *****
  • Posts: 1061
Re: TString problem
« Reply #6 on: July 19, 2022, 01:44:39 pm »
Split is probably the easiest thing to do.

Code: Pascal  [Select][+][-]
  1. var
  2.   i: Integer;
  3.   Row: String;
  4.   Parts: TStringArray;
  5.   S1, S2, S3, S4: String;
  6. begin
  7.   Row :=  '51,40,45,44,44,40,';
  8.   Parts := Row.Split(',');
  9.   S1 := Parts[0];
  10.   S2 := Parts[1];
  11.   S3 := Parts[2];
  12.   S4 := Parts[3];
  13. end;
Don't forget to check the size of Parts  ;)
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: TString problem
« Reply #7 on: July 19, 2022, 01:52:07 pm »
Don't forget to check the size of Parts  ;)
No need to: TStringArray is a dynamic array.
You can also rewrite it like so:
Code: Pascal  [Select][+][-]
  1. var
  2.   i: Integer;
  3.   Row: String = '51,40,45,44,44,40,';
  4.   Parts: TStringArray;
  5.   S1, S2, S3, S4: String;
  6. begin
  7.   Parts := Row.Split(',');
  8.   S1 := Parts[0];
  9.   S2 := Parts[1];
  10.   S3 := Parts[2];
  11.   S4 := Parts[3];
  12. end;
And you can iterate with for..in..do.. which always has the correct size.
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. var
  3.   Row: String = '51,40,45,44,44,40,';
  4.   Parts: TStringArray;
  5.   S: String;
  6. begin
  7.   Parts := Row.Split(',');
  8.   for s in parts do writeln(s);
  9. end.
« Last Edit: July 19, 2022, 02:02:10 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

dseligo

  • Hero Member
  • *****
  • Posts: 1219
Re: TString problem
« Reply #8 on: July 19, 2022, 03:33:08 pm »
Don't forget to check the size of Parts  ;)
No need to: TStringArray is a dynamic array.

Try it with:
Code: Pascal  [Select][+][-]
  1. Row:= '51,40,';

alpine

  • Hero Member
  • *****
  • Posts: 1061
Re: TString problem
« Reply #9 on: July 19, 2022, 03:44:52 pm »
Don't forget to check the size of Parts  ;)
No need to: TStringArray is a dynamic array.
You can also rewrite it like so:
*snip*
It is necessary, and your rewrite actually reflects that need.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TString problem
« Reply #10 on: July 19, 2022, 05:16:09 pm »
Split worked great

Thanks all;
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018