Recent

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

JLWest

  • Hero Member
  • *****
  • Posts: 1169
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: 1086
Re: TString problem
« Reply #1 on: July 19, 2022, 08:46:31 am »
TStringList.CommaText

y.ivanov

  • Hero Member
  • *****
  • Posts: 509
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

  • New Member
  • *
  • Posts: 41
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: 11926
Re: TString problem
« Reply #4 on: July 19, 2022, 10:55:56 am »
Yup, that's how I would do it. Good little example.
Black themes should be banned.

mig-31

  • Sr. Member
  • ****
  • Posts: 299
Re: TString problem
« Reply #5 on: July 19, 2022, 01:06:09 pm »
There is also ExtractDelimited function from StrUtils.
Lazarus 2.0.6 - CentOS 7.x, Mageia 7.1

y.ivanov

  • Hero Member
  • *****
  • Posts: 509
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: 11926
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 »
Black themes should be banned.

dseligo

  • Hero Member
  • *****
  • Posts: 715
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,';

y.ivanov

  • Hero Member
  • *****
  • Posts: 509
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: 1169
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