Skip to content

Instantly share code, notes, and snippets.

@mikasjp
Created April 7, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikasjp/df65c1bd064b433bb5049e72189b7ee3 to your computer and use it in GitHub Desktop.
Save mikasjp/df65c1bd064b433bb5049e72189b7ee3 to your computer and use it in GitHub Desktop.
IsPalindrome C# String extension method example
using System;
using Extensions;
public class MainClass
{
public static void Main (string[] args)
{
var a = "test";
var isPalindrome = a.IsPalindrome();
Console.WriteLine ($"'{ a }' { isPalindrome ? "is palindrome" : "is NOT palindrome" }.");
}
}
using System.Linq;
namespace Extensions
{
public static class StringExtensions
{
public static bool IsPalindrome(this string s)
{
var x = s.ToCharArray()
.AsEnumerable();
var y = x.Reverse();
return x.Zip(y, (m,n) => m == n)
.All(m => m);
}
public static bool IsPalindromeCaseInsensitive(this string s)
{
s = s.ToLowerInvariant();
return s.IsPalindrome();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment