-5

Possible Duplicate:
Using C# to check if string contains a string in string array

Can we have the multiple strings in a single string variable using logical OR operator(|) in asp.net ?

string _Text = "";
string _str = "abc" | "xyz" | "123";
if(_Text.Contains(_str))
{
    foo("_str");
}

Thanks,

Community
  • 1
  • 1
Krishn
  • 11
  • 1
  • 2
  • 3
    Can you do this in any other language? – RvdK Oct 04 '12 at 11:57
  • Have you tryed the code your self? Operator '|' cannot be applied to operands of type 'string' and 'string' – Peter Oct 04 '12 at 11:57
  • @PoweRoy Python allows `"abc" or "xyz" or "123"` with the result `"abc"`, because every non-empty string is evaluated to true. http://ideone.com/STyWh – halex Oct 04 '12 at 11:59
  • http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array – Habib Oct 04 '12 at 12:00
  • its not working, rather array or some other collection will do, but still i want to know is there any way out ... ? – Krishn Oct 04 '12 at 12:01
  • 1
    @halex: I mean 'can you do this in any other language with the desired result', compiled doesn't mean anything. – RvdK Oct 04 '12 at 12:01
  • @PoweRoy Ah ok. Just wanted to show proof of concept of this one line in python :) – halex Oct 04 '12 at 12:03
  • @halex: its not working, thats why i was asking ... – Krishn Oct 04 '12 at 12:04
  • 1
    @krishn, what whould be the meaning of "aaaaa" | "bbb"? – Peter Oct 04 '12 at 12:08
  • @peer: let me rephrase myself, is there any way out to execute function foo(_str), with the same _str value(instead of other _str values) which satisfies the if condition, using string variable ... though we can do the same with an array ... hope i made myself clear. – Krishn Oct 04 '12 at 12:14

6 Answers6

3

use regex Regex.IsMatch(_Text, @"^(abc)|(xyz)|(123)$")

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
2

No, you can't, but you can work around:

string _Text = "abc"
var _str = new[] {"abc", "xyz", "123"};

if(_str.Any(s => _Text.Contains(s))
{
     foo(_Text);
}
cuongle
  • 74,024
  • 28
  • 151
  • 206
1

It's not totally clear to me what you want to do. The or (|) isn't going to work anyway. Do you need something like this?

string haystack = "The quick brown fox jumps over the lazy dog";
string[] needles = {"fox", "the", "dog"};

foreach (var n in needles)
{
    if (haystack.Contains(n))
    {
        Console.WriteLine("'{0}' found in '{1}'", n, haystack);
    }
}   
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

No, you should use any sort of container for that (array/list/etc).

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

No it can't be done your way. But How about using an array

string _Text = "";
string[] _str = {"abc" , "xyz" , "123"};
if(_str.Contains(_Text))
{
    foo(_Text);
}
Peter
  • 27,590
  • 8
  • 64
  • 84
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Both operands have to be booleans:

Logical OR Operator: ||

"The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical OR has left-to-right associativity."

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91