Recent

Author Topic: String Conversion  (Read 1404 times)

pentilisea

  • Guest
String Conversion
« on: February 26, 2023, 04:38:10 pm »
Haya,
still struggling with Syntax ....

Got a txt file to load into a ListBox.

******************
  OpenDialog1.Filter:='txt-Dateien|*.txt';
  if OpenDialog1.Execute then
  ListBox1.Items.LoadFromFile(OpenDialog1.Filename);
*****************

This works OK, but now I need to eliminate certain Items from ListBox1.
The txt files have lines witn Numbers (Integers) and Lines with ASCI (zUklog ---)

I want to deleta all Items containing Characters and
keep all Items containing a Number (1,2,3,4)

Got lost.

************************
for i:=ListBox1.Items.Count - 1 downto 0 do
      if ListBox1.Items :=  ???????????   then               
      ListBox1.Items.Delete(i);
************************

Whatever I am writing instead of the ?????? does not work.

While loading the File the sorting could be done also ....

Would appreciate your hints....

Klaus
 

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: String Conversion
« Reply #1 on: February 26, 2023, 04:52:45 pm »
From your question it isn't clear if you want to delete all lines that whole line is a number or if only contain numbers ('A123B' vs. '123').
Also it isn't clear if you only want digits 1, 2, 3, 4.
Give examples of what you want deleted and what you want to keep.

If you need whole line to be integer number you can do it like this:
Code: Pascal  [Select][+][-]
  1. var iTest: Integer;
  2.  
  3. for i:=ListBox1.Items.Count - 1 downto 0 do
  4.   if not TryStrToInt(ListBox1.Items[i], iTest) then
  5.     ListBox1.Items.Delete(i);


pentilisea

  • Guest
Re: String Conversion (solved)
« Reply #2 on: February 26, 2023, 05:07:08 pm »
Thanks for the fast answer.

The content looks like this:

Datum: 02.03.2022
Version: 1.01
********** Gewinnzahlen **********
S  Z  R
      23
      16
35     
      3
      19
2     
8     
15     
      1
      23

***************************

Now only the numbers, no spaces or text should be selected.

Klaus
« Last Edit: February 26, 2023, 05:19:44 pm by pentilisea »

pentilisea

  • Guest
Re: String Conversion
« Reply #3 on: February 26, 2023, 05:21:09 pm »
Dear dseligo,
thanks so much, works like a charm....

Klaus

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: String Conversion
« Reply #4 on: February 26, 2023, 10:32:51 pm »
Dear dseligo,
thanks so much, works like a charm....

Klaus

I am glad it works for you.

If you need to clear white spaces from beginning and end of string, you could use function Trim (https://www.freepascal.org/docs-html/rtl/sysutils/trim.html), like this:
Code: Pascal  [Select][+][-]
  1.   var iTest: Integer;
  2.  
  3.   for i:=ListBox1.Items.Count - 1 downto 0 do begin
  4.     ListBox1.Items[i] := Trim(ListBox1.Items[i]);
  5.     if not TryStrToInt(ListBox1.Items[i], iTest) then
  6.       ListBox1.Items.Delete(i);
  7.   end;

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2082
  • Fifty shades of code.
    • Delphi & FreePascal
Re: String Conversion
« Reply #5 on: February 26, 2023, 10:49:41 pm »
Just a little performanter when you want to trim.
Code: Pascal  [Select][+][-]
  1. var iTest: Integer;
  2.  
  3. for i:=ListBox1.Items.Count - 1 downto 0 do
  4.   if not TryStrToInt(ListBox1.Items[i], iTest) then
  5.     ListBox1.Items.Delete(i)
  6.     else
  7.     ListBox1.Items[i] := Trim(ListBox1.Items[i]);
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: String Conversion
« Reply #6 on: February 27, 2023, 12:56:41 am »
Just a little performanter when you want to trim.
Code: Pascal  [Select][+][-]
  1. var iTest: Integer;
  2.  
  3. for i:=ListBox1.Items.Count - 1 downto 0 do
  4.   if not TryStrToInt(ListBox1.Items[i], iTest) then
  5.     ListBox1.Items.Delete(i)
  6.     else
  7.     ListBox1.Items[i] := Trim(ListBox1.Items[i]);

Actually this is wrong. If his strings have spaces after the number your code will delete them, and my code will leave them.

@pentilisea: If you want strings to be as they are in the file, then move Trim function to comparison:
Code: Pascal  [Select][+][-]
  1. for i:=ListBox1.Items.Count - 1 downto 0 do
  2.   if not TryStrToInt(Trim(ListBox1.Items[i]), iTest) then
  3.     ListBox1.Items.Delete(i);

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: String Conversion
« Reply #7 on: February 27, 2023, 03:13:14 am »
Just a little performanter when you want to trim.
Code: Pascal  [Select][+][-]
  1. var iTest: Integer;
  2.  
  3. for i:=ListBox1.Items.Count - 1 downto 0 do
  4.   if not TryStrToInt(ListBox1.Items[i], iTest) then
  5.     ListBox1.Items.Delete(i)
  6.     else
  7.     ListBox1.Items[i] := Trim(ListBox1.Items[i]);

Actually this is wrong. If his strings have spaces after the number your code will delete them, and my code will leave them.

@pentilisea: If you want strings to be as they are in the file, then move Trim function to comparison:
Code: Pascal  [Select][+][-]
  1. for i:=ListBox1.Items.Count - 1 downto 0 do
  2.   if not TryStrToInt(Trim(ListBox1.Items[i]), iTest) then
  3.     ListBox1.Items.Delete(i);

Based on your first trim example the above statements/code is very confusing. Are you sure you got things right dseligo ?

Quote
Actually this is wrong. If his strings have spaces after the number your code will delete them, and my code will leave them.
TryStrtoInt does not care about spaces, hence KodeZwerg's code trims strings in the listbox /only/ when they are not deleted (which to me seems the most efficient way of trimming existing items in the listbox) while your first code always trims any string and your second example only supplies a trimmed string to TryStrToInt()

dseligo

  • Hero Member
  • *****
  • Posts: 1222
Re: String Conversion
« Reply #8 on: February 27, 2023, 12:51:07 pm »
Based on your first trim example the above statements/code is very confusing. Are you sure you got things right dseligo ?

Yes, I am sure, I just tried it.

Quote
TryStrtoInt does not care about spaces, hence KodeZwerg's code trims strings in the listbox /only/ when they are not deleted (which to me seems the most efficient way of trimming existing items in the listbox) while your first code always trims any string and your second example only supplies a trimmed string to TryStrToInt()

TryStrToInt doesn't care about spaces in front of number, but it cares about space after.

Try this code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var i: Integer;
  3. begin
  4.   If TryStrToInt('1 ', i) then ShowMessage('dseligo is wrong')
  5.                           else ShowMessage('dseligo is right');
  6. end;

I tried it, guess what is shown?

Lazarus 2.2.4 (rev lazarus_2_2_4) FPC 3.2.2 x86_64-win64-win32/win64

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: String Conversion
« Reply #9 on: February 27, 2023, 01:13:25 pm »
To me, OP's sample file-content looks like something with Columns (CSV?)
And what is that "S Z R"?

In any case: If it's really just the numeric data he wants, please note, he has a duplicate --> 23 is twice in the file, and both times as if it's in Column R
Bottom line: I'm not so sure about everything said, considering "Gewinnzahlen" sounds like Lottery, and Lottery is usually WITHOUT repeat/duplicates
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: String Conversion
« Reply #10 on: February 27, 2023, 06:11:26 pm »
TryStrToInt doesn't care about spaces in front of number, but it cares about space after.
You are correct.

Quote
Try this code:

I tried it, guess what is shown?
Indeed, I was wrong while your statement that TryStrToInt does case about whitespace /after/ a number is correct. Sorry for the confusion.

pentilisea

  • Guest
Re: String Conversion
« Reply #11 on: February 28, 2023, 07:58:19 pm »
Hi Guys,
thanks for the multiple replies, didn't expect all this.

For more understanding: Its Roulette, not Lottery.
I wrote some extensive Gambas3 and am quite at home.
However, I need a bettter integration to Windows, but I'm still struggling with the elevated complecity in Lazarus.

thanks again
Klaus

 

TinyPortal © 2005-2018