Results 1 to 3 of 3

Thread: Logical Help in converting string format.

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Thumbs up 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


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  3. #3

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Re: Logical Help in converting string format.

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width