chore(strings): replace special character

pull/14/head
Ming 2021-01-20 09:25:08 -04:00
parent b7e7e7d272
commit 1a2159d664
1 changed files with 19 additions and 14 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
public class Program public class Program
{ {
@ -8,11 +9,15 @@ public class Program
bool result = false; bool result = false;
result = IsPalindrome("abba"); result = IsPalindrome("abba");
Console.WriteLine(result); Console.WriteLine(result);
result = IsPalindrome("abbccbba"); result = IsPalindrome("abbccbbA");
Console.WriteLine(result);
result = IsPalindrome("Mr. Owl ate my metal worm");
Console.WriteLine(result); Console.WriteLine(result);
} }
private static bool IsPalindrome(string source) { private static bool IsPalindrome(string source) {
source = source.ToLower();
source = Regex.Replace(source, @"[^0-9A-Za-z]", "");
string reverse = new string(Enumerable.Range(1, source.Length).Select(i => source[source.Length - i]).ToArray()); string reverse = new string(Enumerable.Range(1, source.Length).Select(i => source[source.Length - i]).ToArray());
return reverse == source; return reverse == source;
} }