Like Operator in C#

  • Thread starter Thread starter Saradhi
  • Start date Start date
S

Saradhi

I want to check for a conditon as follows:

if ( name Like "sa*")

So, I need to use this * as a wild card and the above statement should return all those names which starts with sa.

I know that VB Like operator will do this one.
I want to know whether we have any similar one in C#?
 
There is no like operator in C#, you will have to use regular expressions.
 
Can you use StartsWith ?

string s = "Sam";
if ( s.StartsWith("Sa") )
//do something.

--
William Stacey, MVP

I want to check for a conditon as follows:

if ( name Like "sa*")

So, I need to use this * as a wild card and the above statement should return all those names which starts with sa.

I know that VB Like operator will do this one.
I want to know whether we have any similar one in C#?
 
Saradhi said:
I want to check for a conditon as follows:

if ( name Like "sa*")

So, I need to use this * as a wild card and the above statement should
return all those names which starts with sa.

C# doesnt have one - but in this simple case just compare the first two
characters.
 
its just an example.

there can be any number of conditons like

name like '*sa*'
name like 'sa*'
name like 'sa%'

Bottom line is I need to implement the Wild Cards in the comparision

Can you use StartsWith ?

string s = "Sam";
if ( s.StartsWith("Sa") )
//do something.

--
William Stacey, MVP

I want to check for a conditon as follows:

if ( name Like "sa*")

So, I need to use this * as a wild card and the above statement should return all those names which starts with sa.

I know that VB Like operator will do this one.
I want to know whether we have any similar one in C#?
 
Daniel O'Connell said:
There is no like operator in C#, you will have to use regular expressions.

This is the answer you are looking for Saradhi.
Daniel gave you a fine piece of advice. Use regular expressions.

- Michael S
 
Saradhi said:
I want to check for a conditon as follows:

if ( name Like "sa*")

So, I need to use this * as a wild card and the above statement should
return all those names which starts with sa.

I know that VB Like operator will do this one.
I want to know whether we have any similar one in C#?

--

Hi,
in case you don't want to use VB specific things regular expressions
can be used, imo regular expressions are essential, I use it regularly

if (System.Text.RegularExpressions.Regex.Match("samsgsgs","sa.*").Success)
MessageBox.Show("");

Frank
 
Back
Top