Click here to Skip to main content
15,889,346 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
7 Feb 2011CPOL 9.7K   2   2
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)


Written By
Software Developer
United States United States
I’m a Software Engineer at Microsoft working on the Azure Portal. Before that I spent about 20 years developed various business applications at a number of different companies. I have a passion for writing clean, scalable code and sharing what I’ve learned with others.

I also help run the Casco Bay .Net User Group

Comments and Discussions

 
GeneralReason for my vote of 3 Short but inefficient. Pin
Björn Friedrich21-Feb-11 20:14
Björn Friedrich21-Feb-11 20:14 
GeneralYou are doing the same thing here as ZamirF. And with regard... Pin
AspDotNetDev7-Feb-11 16:04
protectorAspDotNetDev7-Feb-11 16:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.