It amazes me how many people need to write a function that checks if a word is a palindrome(same forwards as it is backwards). Here is a function that checks if a word or phrase is a palindrome.
The function allows for the user to ignore both the white space and casing.
Code:''' <summary> ''' Checks if a word is the same forward as it is backwards ''' </summary> ''' <param name="value">The value to check.</param> ''' <param name="ignoreWhiteSpace">Determines if whitespace in the value should be ignored.</param> ''' <param name="ignoreCasing">Determines if the casing of the value should be ignored.</param> ''' <returns>Boolean</returns> Private Function IsPalindrome(ByVal value As String, ByVal ignoreWhiteSpace As Boolean, ByVal ignoreCasing As Boolean) As Boolean Dim comparison As StringComparison = If(ignoreCasing, StringComparison.CurrentCultureIgnoreCase, StringComparison.CurrentCulture) If ignoreWhiteSpace Then Return String.Equals(value.Replace(" ", String.Empty), String.Join("", value.Replace(" ", String.Empty).ToArray.Reverse), comparison) Else Return String.Equals(value, String.Join("", value.ToArray.Reverse), comparison) End If End Function




Reply With Quote