Results 1 to 18 of 18

Thread: [RESOLVED] Reversing Numbers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Resolved [RESOLVED] Reversing Numbers

    Hi,

    I have a string that has "13 12 11 10", is there any function that could reverse the string to "10 11 12 13"

    Thanks

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Reversing Numbers

    you could always make a string array and split the string into the array (using the space as a split char) then simple re-assemble the string in the reverse order of the array (dont forget to put the spaces back)
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Hi,
    I have used StrReverse function, but the problem is it is reversing even the numbers;
    for example, I am getting "01" instead of "10" and 31 for 13

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Reversing Numbers

    This is what JuggaloBrotha described.
    VB Code:
    1. Dim str As String = "13 12 11 10"
    2.         Dim numbers() As String = str.Split(CChar(" "))
    3.         str = String.Empty
    4.         For i As Integer = numbers.Length - 1 To 0 Step -1
    5.             str &= numbers(i)
    6.             If i <> 0 Then
    7.                 str &= " "
    8.             End If
    9.         Next

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Hello VBDT,JuggaloBrotha

    Many thanks for your prompt response

    In one of the test cases I have the following string "11 10 9" and am getting "9 01 11" with your code;

    My modified code is

    Dim NR_temp() As String
    NR_temp = Nothing

    Dim MaxString As String
    MaxString = String.Empty

    NR_Temp = "11 10 9" ' Actually a calculation in my program returns this string.

    For j = NR_temp.Length - 1 To 0 Step -1

    MaxString &= NR_temp(j)

    If j <> 0 Then
    MaxString &= " "
    End If

    Next

    Can you please have a look.
    thanks

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Hello,
    Your suggestion seems to be working;
    I forgot to block StrReverse that I used earlier and hence the error..
    Many thanks for your feedback.


    I am wondering if it possible to code rearrange the same string in descending order.

    thanks again...

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Reversing Numbers

    it is not posible if you use exactly the way i showed you. I don't get that, I get "9 10 11" as it spouse to be. The problem might be that you changed some thing.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Hello VBDT,

    Your suggestion seems to be working;
    I forgot to block StrReverse that I used earlier and hence the error..
    Many thanks for your feedback.


    I am wondering if it possible to code rearrange the same string in descending order.

    thanks again...

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Reversing Numbers

    just do the same thing with the same string. Note if you have "11 10 9" and reversed it to "9 10 11" then if you do one more time "9 10 11" will revers to the original "11 10 9".

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reversing Numbers

    VB Code:
    1. Dim str As String = "10 11 12 13"
    2. Dim arr As String() = str.Split(" "c)
    3.  
    4. Array.Reverse(arr)
    5. str = String.Join(" ", arr)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Reversing Numbers

    VB Code:
    1. Dim str As String = "11 13 10 13"
    2. Dim strings As String() = str.Split(" "c)
    3. Dim upperBound As Integer = strings.GetUpperBound(0)
    4. Dim numbers(upperBound) As Integer
    5.  
    6. For i As Integer = 0 To upperBound Step 1
    7.     numbers(i) = Integer.Parse(strings(i))
    8. Next i
    9.  
    10. Array.Sort(numbers, strings) 'Sort ascending.
    11. Array.Reverse(strings) 'Sort descending.
    12. str = String.Join(" ", strings)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Thanks a million VBDT & Jmcilhinney,

    My apologies for not responding earlier.
    I will revert back shortly..

    thanks again...

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Hello jmcilhinney,

    Many thanks for your code for sorting the text inside a string..

    I superimposed your logic in my program, but am getting the following error
    "Input string was not in a correct format."

    VB Code:
    1. Private Function sortNum(ByVal Frame_order As String, ByRef Frame_order_fin As String)
    2.  
    3.         Dim Frame_temp() As String
    4.         Frame_temp = Frame_order.Split(" "c)
    5.  
    6.         Dim upperBound As Integer
    7.         upperBound = Frame_temp.GetUpperBound(0)
    8.  
    9.         Dim No_Of_Mem(upperBound) As Integer
    10.  
    11.         For i As Integer = 0 To upperBound Step 1
    12.             No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
    13.         Next i
    14.  
    15.         Array.Sort(No_Of_Mem, Frame_temp) 'Sort ascending.
    16.         'Array.Reverse(Frame_temp) 'Sort descending.
    17.  
    18.         Frame_order_fin = String.Join(" ", Frame_temp)
    19.  
    20.         Return Frame_order_fin
    21.  
    22.     End Function

    Can you please advice..

    thanks

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    The error is in this line

    VB Code:
    1. For i As Integer = 0 To upperBound Step 1
    2.             No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
    3.  Next i

  15. #15
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Reversing Numbers

    It could mean that Frame_temp(i) can't be parsed as an Integer.

    Have you turned Option Strict and Explicit On?

    Have you outputted Frame_Temp() to see what kind of format it is actually in? Some of it may not be splitting how you intended for example. Or if there is more than 1 space it will load one element of the array with nothing - hence one possible reason for the error.

    Perhaps it would be better if you used the TryParse method (if using 2005...) and if it returns True then assign your variable the value.

    Place the code in a Try/Catch block, it may help with the debug.
    Last edited by stimbo; Feb 13th, 2007 at 07:37 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  16. #16
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Reversing Numbers

    let's make your function an actual function first:
    VB Code:
    1. Private Function sortNum(ByVal Frame_order As String) As String
    2.  
    3.         Dim Frame_temp() As String = Frame_order.Split(" "c)
    4.  
    5.         Dim upperBound As Integer = Frame_temp.GetUpperBound(0)
    6.  
    7.         Dim No_Of_Mem(upperBound) As Integer
    8.  
    9.         For i As Integer = 0 To upperBound Step 1
    10.             No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
    11.         Next i
    12.  
    13.         Array.Sort(No_Of_Mem, Frame_temp) 'Sort ascending.
    14.         'Array.Reverse(Frame_temp) 'Sort descending.
    15.  
    16.         Return String.Join(" ", Frame_temp)
    17.        
    18.     End Function
    Last edited by JuggaloBrotha; Feb 13th, 2007 at 08:27 PM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  17. #17
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Reversing Numbers

    You need to remove any empty string in the split function... Otherwise you'll get an exception when you try to parse an empty string.
    So chnge this line
    VB Code:
    1. Dim Frame_temp() As String = Frame_order.Split(" "c)
    To this then you should be all set
    VB Code:
    1. Dim Frame_temp() As String = Frame_order.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    449

    Re: Reversing Numbers

    Thanks a Ton to all of you......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width