Recent

Author Topic: [SOLVED]Check if char exists in a predefined array  (Read 3376 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
[SOLVED]Check if char exists in a predefined array
« on: January 11, 2021, 07:25:34 pm »
Using Lazarus 2.0.10, on win 8.1

Code: Pascal  [Select][+][-]
  1. Const:
  2. MyArray : array[0..3] of char = ('A','N','S','O');
  3.  
  4. s : string = 'LWFEUÑOWWÑLKDWE';
  5. for counter := 1 to length(s) do
  6.    // if s[counter] exist in MyArray do something

How i can check i s[counter] is valid char in myarray? i could add another counter to check if s[counter] = MyArray[counter2], but i want know if there is a simple way to do it.

Thanks.
« Last Edit: January 11, 2021, 09:36:23 pm by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Check if char exists in a predefined array
« Reply #1 on: January 11, 2021, 07:30:34 pm »
IIRC then fpc trunk allows the "in" operator for that (which internally will I think use a loop.
If you make MyArray a string then you can use Pos() to determine if a char is in MyArray.
Internally this uses more optimized code than a simple for loop IIRC, so it might be faster.

Bart

ASerge

  • Hero Member
  • *****
  • Posts: 2499
Re: Check if char exists in a predefined array
« Reply #2 on: January 11, 2021, 08:56:57 pm »
You can also use sets if the array does not contain "long" characters.
And I would recommend write a separate CharInArray function, and then choosing it implementation.

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Check if char exists in a predefined array
« Reply #3 on: January 11, 2021, 09:36:04 pm »
Thanks for the replys:

The pos function using a string is a great solution!
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Zvoni

  • Hero Member
  • *****
  • Posts: 3399
Re: [SOLVED]Check if char exists in a predefined array
« Reply #4 on: January 12, 2021, 08:21:41 am »
The way i do it:
Loop through MyArray (instead of your target-string) and use StrScan
https://www.freepascal.org/docs-html/current/rtl/strings/strscan.html
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

PascalDragon

  • Hero Member
  • *****
  • Posts: 6398
  • Compiler Developer
Re: [SOLVED]Check if char exists in a predefined array
« Reply #5 on: January 12, 2021, 08:53:42 am »
The way i do it:
Loop through MyArray (instead of your target-string) and use StrScan
https://www.freepascal.org/docs-html/current/rtl/strings/strscan.html

Better use Pos. Pascal strings are allowed to have NUL characters, while StrScan works on NUL-terminated strings, thus will stop on the first NUL found.

Zvoni

  • Hero Member
  • *****
  • Posts: 3399
Re: [SOLVED]Check if char exists in a predefined array
« Reply #6 on: January 12, 2021, 09:48:53 am »
The way i do it:
Loop through MyArray (instead of your target-string) and use StrScan
https://www.freepascal.org/docs-html/current/rtl/strings/strscan.html

Better use Pos. Pascal strings are allowed to have NUL characters, while StrScan works on NUL-terminated strings, thus will stop on the first NUL found.
PD,
i know that.
That's why i wrote "The way i do it", since i have full control of "my" strings.....
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

 

TinyPortal © 2005-2018