Palindromes in Visual Basic.NET

Welcome to today's article. Today, you are going to learn how to detect palindromes from within your VB.NET application.


A palindrome is a word, phrase, or number which reads the same backward as forward. There are different types of palindromes, including the following:

  • Characters and words
  • Sentences and phrases
  • Names
  • Numbers
  • Semordnilap

Characters and Words

Character palindromes read the same backward as forward. Noon, civic, radar, level, and kayak are good examples of character palindromes. Word-unit palindromes are palindromes in which the unit of reversal is simply the word. For example, the following is a word-unit palindrome: Blessed are they that believe that they are blessed.

Sentences and Phrases

Palindromic sentences consist of a sentence or phrase, for example: Live on time, emit no evil.


Some given names are palindromes, for example: Hannah, Anna, and Bob.


Semordnilap (palindromes spelled backward)—also known as: word reversals, reversible anagrams, heteropalindromes, or a semi-palindrome—spell a different word in reverse. For example: desserts, which is stressed backward, or vice versa.


In today's project, you will learn four different ways to identify a potential palindrome. Create a new Visual Basic Windows Forms project and design it as shown in Figure 1. You may give your objects different names than mine.

Figure 1: Design


Add the following code behind the button labeled 'Option 1':

   Private Sub Button1_Click(sender As Object, e As EventArgs) _
         Handles Button1.Click

      'Option 1'
      Dim strText As String = TextBox1.Text

      strText = StrReverse(strText)

      If strText.Equals(strText) Then



         MessageBox.Show("Not Palindrome")

      End If

   End Sub

This is relatively easy. It makes use of the built-in StrReverse string function to reverse the input; then, it simply compares the reversed string with the input string.

Add the following code for the button labeled 'Option 2':

   Public Function Palindrome2(strText As String) As Boolean

      'Option 2'
      Dim intCurr As Integer = 0
      Dim intLength As Integer = strText.Length - 1

      While intCurr < intLength

         If strText(intCurr) <> strText(intLength) Then

            Return False

         End If

         intCurr += 1
         intLength -= 1

      End While

      Return True

      End Function

   Private Sub Button2_Click(sender As Object, e As EventArgs) _
         Handles Button2.Click


   End Sub

This method basically counts the letters from the front to the back, thus resulting in a True or False value.

Add 'Option 3':

   Public Function Palindrome3(strText As String) As Boolean

      'Option 3'
      Dim blnPal As Boolean = True

      For i As Integer = 0 To strText.Length \ 2

         If strText(i) <> strText(strText.Length - i - 1) Then

            blnPal = False

            Exit For

         End If


      Return blnPal

   End Function

   Private Sub Button3_Click(sender As Object, e As EventArgs) _
         Handles Button3.Click


   End Sub

Add the last method:

   Private Function Palindrome4(ByVal strText As String, _
         ByVal blnWhiteSpace As Boolean, _
         ByVal blnCase As Boolean) As Boolean

      'Option 4'
      Dim scCompare As StringComparison = If(blnCase, _
         StringComparison.CurrentCultureIgnoreCase, _

      If blnWhiteSpace Then

         Return String.Equals(strText.Replace(" ", String.Empty), _
            String.Join("", strText.Replace(" ", String.Empty) _
            .ToArray.Reverse), scCompare)


         Return String.Equals(strText, String.Join("", _
            strText.ToArray.Reverse), scCompare)

      End If

   End Function
   Private Sub Button4_Click(sender As Object, e As EventArgs) _
         Handles Button4.Click

      MessageBox.Show((Palindrome4(TextBox1.Text, True, True)))

   End Sub

Method 4 caters to Whitespace and case sensitivity, if needed.

The code for this article is available on GitHub.


Palindromes can be tricky. Languages can be tricky. Hopefully, with this article you have learned how to properly manipulate strings to check for palindromes.

This article was originally published on December 1st, 2017

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date