Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

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

4.67/5 (4 votes)
7 Feb 2011CPOL 10.2K  
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:

C#
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);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)