|
-
Feb 9th, 2007, 01:44 AM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 9th, 2007, 01:52 AM
#2
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)
-
Feb 9th, 2007, 01:56 AM
#3
Thread Starter
Hyperactive Member
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
-
Feb 9th, 2007, 02:16 AM
#4
Re: Reversing Numbers
This is what JuggaloBrotha described.
VB Code:
Dim str As String = "13 12 11 10"
Dim numbers() As String = str.Split(CChar(" "))
str = String.Empty
For i As Integer = numbers.Length - 1 To 0 Step -1
str &= numbers(i)
If i <> 0 Then
str &= " "
End If
Next
Last edited by VBDT; Feb 9th, 2007 at 02:26 AM.
-
Feb 9th, 2007, 02:34 AM
#5
Thread Starter
Hyperactive Member
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
-
Feb 9th, 2007, 02:37 AM
#6
Thread Starter
Hyperactive Member
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...
-
Feb 9th, 2007, 02:42 AM
#7
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.
-
Feb 9th, 2007, 02:43 AM
#8
Thread Starter
Hyperactive Member
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...
-
Feb 9th, 2007, 02:55 AM
#9
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".
-
Feb 9th, 2007, 02:58 AM
#10
Re: Reversing Numbers
VB Code:
Dim str As String = "10 11 12 13"
Dim arr As String() = str.Split(" "c)
Array.Reverse(arr)
str = String.Join(" ", arr)
-
Feb 9th, 2007, 03:02 AM
#11
Re: Reversing Numbers
VB Code:
Dim str As String = "11 13 10 13"
Dim strings As String() = str.Split(" "c)
Dim upperBound As Integer = strings.GetUpperBound(0)
Dim numbers(upperBound) As Integer
For i As Integer = 0 To upperBound Step 1
numbers(i) = Integer.Parse(strings(i))
Next i
Array.Sort(numbers, strings) 'Sort ascending.
Array.Reverse(strings) 'Sort descending.
str = String.Join(" ", strings)
-
Feb 13th, 2007, 01:08 AM
#12
Thread Starter
Hyperactive Member
Re: Reversing Numbers
Thanks a million VBDT & Jmcilhinney,
My apologies for not responding earlier.
I will revert back shortly..
thanks again...
-
Feb 13th, 2007, 05:32 AM
#13
Thread Starter
Hyperactive Member
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:
Private Function sortNum(ByVal Frame_order As String, ByRef Frame_order_fin As String)
Dim Frame_temp() As String
Frame_temp = Frame_order.Split(" "c)
Dim upperBound As Integer
upperBound = Frame_temp.GetUpperBound(0)
Dim No_Of_Mem(upperBound) As Integer
For i As Integer = 0 To upperBound Step 1
No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
Next i
Array.Sort(No_Of_Mem, Frame_temp) 'Sort ascending.
'Array.Reverse(Frame_temp) 'Sort descending.
Frame_order_fin = String.Join(" ", Frame_temp)
Return Frame_order_fin
End Function
Can you please advice..
thanks
-
Feb 13th, 2007, 05:44 AM
#14
Thread Starter
Hyperactive Member
Re: Reversing Numbers
The error is in this line
VB Code:
For i As Integer = 0 To upperBound Step 1
No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
Next i
-
Feb 13th, 2007, 06:02 AM
#15
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.
-
Feb 13th, 2007, 12:16 PM
#16
Re: Reversing Numbers
let's make your function an actual function first:
VB Code:
Private Function sortNum(ByVal Frame_order As String) As String
Dim Frame_temp() As String = Frame_order.Split(" "c)
Dim upperBound As Integer = Frame_temp.GetUpperBound(0)
Dim No_Of_Mem(upperBound) As Integer
For i As Integer = 0 To upperBound Step 1
No_Of_Mem(i) = Integer.Parse(Frame_temp(i))
Next i
Array.Sort(No_Of_Mem, Frame_temp) 'Sort ascending.
'Array.Reverse(Frame_temp) 'Sort descending.
Return String.Join(" ", Frame_temp)
End Function
Last edited by JuggaloBrotha; Feb 13th, 2007 at 08:27 PM.
-
Feb 13th, 2007, 12:46 PM
#17
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:
Dim Frame_temp() As String = Frame_order.Split(" "c)
To this then you should be all set
VB Code:
Dim Frame_temp() As String = Frame_order.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
-
Feb 15th, 2007, 12:34 AM
#18
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|