-2

How can i get more values in a EndsWith?

For example

if(textbox1.Text.EndsWith("hotmail.com" && "anothermail.com"){

}

What to do?

Kvist
  • 29
  • 4

4 Answers4

5

You can create an extension method:

public static bool EndsWithAny(this string str, params string[] search)
{
    return search.Any(s => str.EndsWith(s));
}

//Call
bool found = str.EndsWithAny("hotmail.com", "anothermail.com");
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
2

If you want to get the results where the value ends with one string, or ends with the other string, then combine the two checks with a logical or:

if(textbox1.Text.EndsWith("hotmail.com") || textbox1.Text.EndsWith("anothermail.com")) { ... }
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1
string[] ends = new string[] { "hotmail.com" , "anothermail.com" };
string text = "example.hotmail.com2";
bool endsWith = ends.Any(e => text.EndsWith(e));
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
1

You could use a collection and Any+EndsWith:

var mails = new[] { "hotmail.com", "anothermail.com" };
bool endsWithAny= mails.Any(textbox1.Text.EndsWith);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939