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
Printable View
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
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)
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
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
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
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...
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.
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...
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".
VB Code:
Dim str As String = "10 11 12 13" Dim arr As String() = str.Split(" "c) Array.Reverse(arr) str = String.Join(" ", arr)
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)
Thanks a million VBDT & Jmcilhinney,
My apologies for not responding earlier.
I will revert back shortly..
thanks again...
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
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
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.
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
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
To this then you should be all setVB Code:
Dim Frame_temp() As String = Frame_order.Split(" "c)
VB Code:
Dim Frame_temp() As String = Frame_order.Split(" ".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
Thanks a Ton to all of you......