Created
April 7, 2019 11:11
-
-
Save mikasjp/df65c1bd064b433bb5049e72189b7ee3 to your computer and use it in GitHub Desktop.
IsPalindrome C# String extension method example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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