|
-
May 27th, 2009, 01:51 AM
#1
Thread Starter
Fanatic Member
Logical Help in converting string format.
I am in need to convert as below.
Source string :
1,2,6,2-5,10-15
Destination String :
;1;2;6;3;4;5;10;11;12;13;14;15;
in destination string, duplicates not allowed.
Any help appreciated..
Visual Studio.net 2010
If this post is useful, rate it

-
May 27th, 2009, 01:57 AM
#2
Re: Logical Help in converting string format.
1. Use String.Split to split the original string on the commas.
2. Create an empty List to store the values.
3. For each element in the split array, split it on the dash.
4. If there's only one element there was no dash. You can just test the List and, if the value isn't present, add it.
5. If there are two elements then there was a dash so you can loop from the first value to the second value.
6. For each value you can, again, test the List and add the value.
-
May 27th, 2009, 02:11 AM
#3
Thread Starter
Fanatic Member
Re: Logical Help in converting string format.
 Originally Posted by jmcilhinney
1. Use String.Split to split the original string on the commas.
2. Create an empty List to store the values.
3. For each element in the split array, split it on the dash.
4. If there's only one element there was no dash. You can just test the List and, if the value isn't present, add it.
5. If there are two elements then there was a dash so you can loop from the first value to the second value.
6. For each value you can, again, test the List and add the value.
Thanks jmc for the steps...
Code:
Private Function GetSemiColonSeperatedValue(ByVal sCommaSeperatedValue As String) As String
Dim sOutString As String = ";"
Try
For Each sTempComma As String In sCommaSeperatedValue.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
If sTempComma.Contains("-") Then
Dim sArr() As String = sTempComma.Split(New Char() {"-"c}, StringSplitOptions.RemoveEmptyEntries)
Dim iMinRange As Integer = Integer.Parse(sArr(0))
Dim iMaxRange As Integer = Integer.Parse(sArr(1))
If iMinRange <= iMaxRange Then
For iTemp As Integer = iMinRange To iMaxRange
If sOutString.Contains(";" & iTemp & ";") = False Then
sOutString &= iTemp & ";"
End If
Next
End If
Else
If sOutString.Contains(";" & sTempComma & ";") = False Then
sOutString &= sTempComma & ";"
End If
End If
Next
Catch ex As Exception
Return String.Empty
End Try
Return sOutString
End Function
this working fine..

Visual Studio.net 2010
If this post is useful, rate it

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
|