Solved

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

Posted on 2015-02-12
6
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40607394
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 101

Expert Comment

by:mlmcc
ID: 40607899
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 27

Expert Comment

by:Sinisa Vuk
ID: 40607968
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Closing Comment

by:shawn857
ID: 40609094
Thanks to all who responded.

Cheers
   Shawn
0
 
LVL 101

Expert Comment

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

mlmcc
0
 

Author Comment

by:shawn857
ID: 40611738
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

631 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