Solved

Check if char variable is in an array using a "set" type comparison

Posted on 2015-02-12
6
128 Views
Last Modified: 2015-02-15
Hi all, I'm doing some "set type" comparisons in my app like this:

if h1 in ['0'..'9'] then....

Open in new window


.. where h1 is just a variable of char. No problem there, but I have a string array that contains a collection of punctuation characters ('!','#','.','?','%' etc) and I'm wondering if there's a comparable way (ie. using the "in" operator) to check if my variable h1 contains any of these characters. As in:

if h1 in PunctuationArray

Open in new window


... or something like that.

Thanks!
    Shawn
0
Comment
Question by:shawn857
6 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
Hi,

have you tried AnsiMatchStr or MatchStr

source := 'Henry';  
 if AnsiMatchStr(source, ['Brian', 'Jim', 'Henry'])
  then ShowMessage('First match was successful')
  else ShowMessage('First match was not successful');


http://www.delphibasics.co.uk/RTL.asp?Name=AnsiMatchStr

Regards
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
So if h1 is a string   and your set is like above ['!','#','.','?','%', ',' ] you want to see if any of the punctuation characters are in the string, h1?

So if h1 = "Peter and Paul" the answer is false but if it is "Peter, Paul, and Mary" it is true.

In that case code like this should find it

Haschar := False;
For I := 0  to High(YourArray) 
    If POS(YourArray[I], h1) then
       Haschar := True;

Open in new window

mlmcc
0
 
LVL 25

Expert Comment

by:Sinisa Vuk
Comment Utility
Similar to upper example - iterate your array and instead of pos (which can check only one char in time)
use function CheckString from this question: Q_28229176
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Closing Comment

by:shawn857
Comment Utility
Thanks to all who responded.

Cheers
   Shawn
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
How does that answer the issue or did I not understand your question?

mlmcc
0
 

Author Comment

by:shawn857
Comment Utility
Hi mlmcc... you were looking at it backwards I think. Variable h1 contains just one character, and I want to see if that character is contained anywhere in the set ['!','#','.','?','%', ',' ]

Thanks for contributing though...

Cheers
   Shawn
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now