473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2889
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.