locked
String splitting into an array RRS feed

  • Question

  • Is there any way in splitting a string and creating an array without the spaces?

    For example i want to make "99" into "9","9"

    In my program I am trying to see whether a number is palindromic (reads the same way forwards as it does backwards like the name "hannah"), and I am trying to do this by converting the number to a string, making the number the reverse of itself by swapping the values and then comparing that string to the original.

    Friday, April 11, 2014 2:17 PM

Answers

  • Hi, you can use String.ToCharArray() to get char[] and then you can compare char[]

    Dim number = 12321
                Dim s = number.ToString()
                If s.ToCharArray().SequenceEqual(s.ToCharArray().Reverse()) Then
                    MsgBox("palindromic ")
                End If

    • Edited by lapheal Friday, April 11, 2014 2:28 PM
    • Proposed as answer by .paul. _ Friday, April 11, 2014 6:13 PM
    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:40 AM
    Friday, April 11, 2014 2:23 PM
  •         Dim mystring, teststring, reverstring As String
            Console.Write("Enter your string:  ")
            mystring = Console.ReadLine()
            teststring = mystring
            Do
                teststring = teststring.Replace(" ", "")
            Loop While teststring.Contains(" ")
            reverstring = StrReverse(teststring)
            If teststring.ToLower() = reverstring.ToLower() Then
                Console.WriteLine("Your string is a palindrome.")
            Else
                Console.WriteLine("Your string is not a palindrome.")
            End If


    Solitaire

    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:42 AM
    Sunday, April 13, 2014 12:43 AM
  • you can also use this:

    If number.ToString.ToArray = number.ToString.Reverse.ToArray

    • Proposed as answer by IronRazerz Saturday, April 12, 2014 3:26 PM
    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:40 AM
    Saturday, April 12, 2014 2:23 PM

All replies

  • Hi, you can use String.ToCharArray() to get char[] and then you can compare char[]

    Dim number = 12321
                Dim s = number.ToString()
                If s.ToCharArray().SequenceEqual(s.ToCharArray().Reverse()) Then
                    MsgBox("palindromic ")
                End If

    • Edited by lapheal Friday, April 11, 2014 2:28 PM
    • Proposed as answer by .paul. _ Friday, April 11, 2014 6:13 PM
    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:40 AM
    Friday, April 11, 2014 2:23 PM
  • you can also use this:

    If number.ToString.ToArray = number.ToString.Reverse.ToArray

    • Proposed as answer by IronRazerz Saturday, April 12, 2014 3:26 PM
    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:40 AM
    Saturday, April 12, 2014 2:23 PM
  •         Dim mystring, teststring, reverstring As String
            Console.Write("Enter your string:  ")
            mystring = Console.ReadLine()
            teststring = mystring
            Do
                teststring = teststring.Replace(" ", "")
            Loop While teststring.Contains(" ")
            reverstring = StrReverse(teststring)
            If teststring.ToLower() = reverstring.ToLower() Then
                Console.WriteLine("Your string is a palindrome.")
            Else
                Console.WriteLine("Your string is not a palindrome.")
            End If


    Solitaire

    • Marked as answer by Carl Cai Thursday, April 17, 2014 9:42 AM
    Sunday, April 13, 2014 12:43 AM