To check string is palindrome or not in .NET (C#)






4.67/5 (4 votes)
There's no need to compare each character of the string separately:public static bool IsPalindrome(string str, StringComparison comparisonType){ if (string.IsNullOrEmpty(str)) return false; string str2 = new string(str.Reverse().ToArray()); return string.Equals(str, str2,...
There's no need to compare each character of the
string
separately:
public static bool IsPalindrome(string str, StringComparison comparisonType)
{
if (string.IsNullOrEmpty(str)) return false;
string str2 = new string(str.Reverse().ToArray());
return string.Equals(str, str2, comparisonType);
}
public static bool IsPalindrome(string str)
{
return IsPalindrome(str, StringComparison.OrdinalIgnoreCase);
}