473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# if string match found boolean - streamlined code?

Though this code appears to work, I suspect it could be streamline.
Total Noob Here.
public bool isit(string c1)
{
string color1 = "blue green red";
Regex re = new Regex("@"+c1, RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if (re.Matches(color1).Count 0)
{
return (true);
}
else
{
return (false);
}
==
I found a sample on the net that does this

if (re.color1 != 0 )

But I get an error about Regex not having a definition for color1

==
Also, what if color1 was an enum, how would test the search string can
be found in any of the items?

Aug 29 '06 #1
5 2908
how about

switch(c1) {
case "blue":
case "green":
case "red":
return true;
default:
return false;
}

More generally, you could use
return re.IsMatch(color1);

If it was an enum, then the Enum helper class can do a lot (i.e. list
available items, parse items, etc); e.g.

enum MyColors { Blue, Red, Green };
static void Main(string[] args) {
Console.WriteLine(IsColor("blue"));
Console.WriteLine(IsColor("red"));
MyColors value;
if (TryMatchColor("green", out value)) {
Console.WriteLine(value);
}
if (TryMatchColor("turquoise", out value)) {
Console.WriteLine(value);
}
}
static bool IsColor(string name) {
MyColors color;
return TryMatchColor(name, out color);
}
static bool TryMatchColor(string name, out MyColors value) {
try {
value = (MyColors) Enum.Parse(typeof(MyColors), name, true);
return true;
} catch (ArgumentException) {
value = 0; // default
return false;
}
}
Aug 29 '06 #2
OK - reading again I may have missed your intention... forgetting about
solving it via Regex (or any other approach) - what exactly are you trying
to do? i.e. input {x}, output {y}...?

Marc
Aug 29 '06 #3
public bool isit(string c1)
{
string[] color1 = new string[] {"blue", "green", "red"};
return (Array.IndexOf<string>(color1, c1.ToLower()) -1);
}

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
<ja***@cyberpine.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
Though this code appears to work, I suspect it could be streamline.
Total Noob Here.
public bool isit(string c1)
{
string color1 = "blue green red";
Regex re = new Regex("@"+c1, RegexOptions.IgnoreCase |
RegexOptions.Multiline);
if (re.Matches(color1).Count 0)
{
return (true);
}
else
{
return (false);
}
==
I found a sample on the net that does this

if (re.color1 != 0 )

But I get an error about Regex not having a definition for color1

==
Also, what if color1 was an enum, how would test the search string can
be found in any of the items?

Aug 29 '06 #4

Marc Gravell wrote:
OK - reading again I may have missed your intention... forgetting about
solving it via Regex (or any other approach) - what exactly are you trying
to do? i.e. input {x}, output {y}...?

Marc
The post with the array is going to work.. I wanted to use Regex
because some of the entries will be sentences and I might need to
search for patterns. I know I just asked for a boolean, but Ideally I
wanted to return an array of every entry where the pattern was found.

Thank you!

Aug 30 '06 #5

Kevin Spencer wrote:
public bool isit(string c1)
{
string[] color1 = new string[] {"blue", "green", "red"};
return (Array.IndexOf<string>(color1, c1.ToLower()) -1);
}

Thank you.. this is a great help.

if you have it handy.. what if the enteries were very long sentences
and It was a pattern I was searching for (I simplified my original
question). And what if I wanted to return an array of lines that had
the match. I know I might be pushing it and if so I'll go dig around -
I have a very similar VB code I'm trying to migrate over.

Thanks again - really appreciate it!

Aug 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
5212
by: Kin®sole | last post by:
Hi I'm very new to VB (using VB6) I have two lists one blank and one containing names in the format of surname and then forename.I also have a combo box containing forenames.When I select a...
2
2384
by: gyromagnetic | last post by:
Hi, I have written a function that searches a text string for various words. The text is searched using a boolean 'and' or a boolean 'or' of the input list of search terms. Since I need to use...
5
7690
by: Nasos Makriyiannis | last post by:
Hi, I'm new to XSL and I was wondering if there is a string-compare function available. I am using the following IF statement but it does not seem to be working: <xsl:if...
29
4277
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
0
25849
by: phmyhn | last post by:
I have two web pages, one is viewlarger.aspx, another one is shoppingcart.aspx. On the viewlarger.aspx, when clicking "add to cart" image button, the sub appends the id (passed from another page...
9
7110
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
0
249
by: jason | last post by:
Though this code appears to work, I suspect it could be streamline. Total Noob Here. public bool isit(string c1) { string color1 = "blue green red"; Regex re = new Regex("@"+c1,...
20
1895
by: arnimavidyarthy | last post by:
Hi, Hi, I am taking a string as an input from the user and it should only contain the chars:L , M or R I tried the folllowing in kodos but they are still not perfect:
9
4550
by: WebArchitect | last post by:
I have a text file. My text file contains lines with double quotes around it. I trying to code if a line has double quotes around it Then make it bold My code is below and it is not working....
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.