2
string currentUrl = "destination/India/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

In the above example,In C# how can i check that Country name is repeating in url?

2
  • contains method? Commented Jul 13, 2018 at 7:05
  • 2
    What did you try? Commented Jul 13, 2018 at 7:06

2 Answers 2

5

By Count()ing the appearence of countryName

string currentUrl = "destination/India/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";
bool isRepeating = array.Count(x => countryName.Equals(x, StringComparison.OrdinalIgnoreCase)) > 1;
Sign up to request clarification or add additional context in comments.

Comments

1

you can use the Distinct method. Something like

string currentUrl = "destination/India/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

if (array.Distinct().Count() != array.Count())
{
    Console.WriteLine("Duplicate");
}

Or you can try blow code

string currentUrl = "destination/india/india/mumbai";
string[] array = currentUrl.Split('/');
string countryName = "india";

bool r = array.Where(x => x.Equals(countryName)).Count()>1;
if(r)
{
    Console.WriteLine("Duplicate");
}

3 Comments

could you please elaborate more about you problem ?
"destination/destination/India/mumbai" will also result duplicate but the country name is not repeating
You can use this bool r= array.Where(x => x.Equals(countryName)).Count()>1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.