Results 1 to 15 of 15

Thread: Unique only data in string

  1. #1

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Unique only data in string

    My goal is to make the string ...

    vb Code:
    1. strLocations = "WI - WI -  WI -  IL -  IL"

    to

    vb Code:
    1. strLocations = "WI - IL"


    I'm restricted to using the 2.0 framework as well.

    Any ideas or help would be greatly appreciated.

  2. #2

    Re: Unique only data in string

    Is this a homework assignment? I'm just curious.

  3. #3
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Unique only data in string

    VB.Net Code:
    1. Dim strLocations As String = "WI - WI -  WI -  IL -  IL"
    2. strLocations = Strings.Left(strLocations, 2) & " - " & Strings.Right(strLocations, 2)
    3. MsgBox(strLocations)  'Result in MsgBox is "WI - IL"
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Unique only data in string

    Code:
    Dim str As String = "WI - WI -  WI -  IL -  IL"
    Dim idx As Integer = str.IndexOf("  ")
    While idx > -1
         str = str.Remove(idx,1)
         idx = str.IndexOf("  ")
    End While
    Dim split() As String = str.Split("-"c)
    Dim al As New ArrayList(), s As String, ns As String = String.Empty
    For Each s In split
         Dim tr As String = s.Trim()
         If al.IndexOf(tr) = -1 Then
              al.Add(tr)
              ns &= " - " & tr
         End If
    Next
    ns = ns.TrimStart(New Char() {" "c,"-"c})
    MessageBox.Show(ns)
    @Zeljko: Is the data really going to be the same each time???
    + The class is STRING, not STRINGS.

  5. #5

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Unique only data in string

    Quote Originally Posted by formlesstree4 View Post
    Is this a homework assignment? I'm just curious.
    I've been here long enough to know better.




    This is for a program that fills loads for trucks. Each truck has orders. Each order can be dropped off in different states.

    The end user's wish not to see duplicate state stops.

    Sometimes the string could be MI - WI - IL - MI and they want to see MI - WI - IL instead of seeing MI twice.

    This is nationwide so it could be any assortment of states.

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

    Re: Unique only data in string

    You need to write a little function that does this:
    1. splits the string by " - "
    2. loop through the returned array and adds unique items to a list of strings
    3. build the string from list adding " - " between the states
    4. return the string.

  7. #7

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Unique only data in string

    Quote Originally Posted by VBDT View Post
    You need to write a little function that does this:
    1. splits the string by " - "
    2. loop through the returned array and add unique items to a list of strings
    3. build the string from list adding " - " between the states
    4. return the string.
    That's what I've come down to, but I'm not skilled enough. I tried quite a few times, but it fails each time. So I did more searching on it and still no luck.

    The splitting was the easy part.
    Looping through, adding unique items ... for some reason I fail at that.

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

    Re: Unique only data in string

    Here is an example:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim str As String = "WI - WI -  WI -  IL -  IL"
            Dim newStr As String = Me.GetStatsString(str)
        End Sub
    
        Private Function GetStatsString(ByVal statesStr As String) As String
            Dim states() As String = statesStr.Split(New String() {" - "}, StringSplitOptions.RemoveEmptyEntries)
            Dim stateList As New List(Of String)
            For Each state As String In states
                state = state.Trim
                If Not stateList.Contains(state) Then
                    stateList.Add(state)
                End If
            Next
            Dim newStatesStr As String = String.Empty
            For i As Integer = 0 To stateList.Count - 1
                If i = 0 Then
                    newStatesStr = stateList(i)
                Else
                    newStatesStr &= " - " & stateList(i)
                End If
            Next
            Return newStatesStr
        End Function
    
    End Class

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

    Re: Unique only data in string

    And this is the optimized version of the function (the second loop is gone):
    Code:
    Private Function GetStatsString(ByVal statesStr As String) As String
        Dim states() As String = statesStr.Split(New String() {" - "}, StringSplitOptions.RemoveEmptyEntries)
        Dim stateList As New List(Of String)
        Dim newStatesStr As String = String.Empty
        For Each state As String In states
            state = state.Trim
            If Not stateList.Contains(state) Then
                stateList.Add(state)
                If stateList.Count = 1 Then
                    newStatesStr = state
                Else
                    newStatesStr &= " - " & state
                End If
            End If
        Next
        Return newStatesStr
    End Function

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Unique only data in string

    Did everyone just ignore my post?

    Code:
    Dim str As String = "WI - WI -  WI -  IL -  IL"
    Dim idx As Integer = str.IndexOf("  ")
    While idx > -1
         str = str.Remove(idx,1)
         idx = str.IndexOf("  ")
    End While
    Dim split() As String = str.Split("-"c)
    Dim al As New ArrayList(), s As String, ns As String = String.Empty
    For Each s In split
         Dim tr As String = s.Trim()
         If al.IndexOf(tr) = -1 Then
              al.Add(tr)
              ns &= " - " & tr
         End If
    Next
    ns = ns.TrimStart(New Char() {" "c,"-"c})
    MessageBox.Show(ns)
    I didn't know the Of keyword was available in 2.0. Anyways, if you have it, replace ArrayList with List(Of String).
    Last edited by minitech; May 4th, 2010 at 09:13 PM.

  11. #11

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Unique only data in string

    minitech, I assure you, I have not ignored your post. I'm not at my desk right now. I'll check them out in the morning and reply.


    VBDT, can you tell me what '&=' is used for?

    vb Code:
    1. newStatesStr &= " - " & stateList(i)


    Thanks for all of the suggestions guys!

  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Unique only data in string

    &= is shorthand for ... = ... & ... .
    So you can append a string to another with shorthand.

  13. #13

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Unique only data in string

    Quote Originally Posted by minitech View Post
    &= is shorthand for ... = ... & ... .
    So you can append a string to another with shorthand.
    So,
    vb.net Code:
    1. Dim strString as string = "test"
    2. strString = strString & " this"

    is the same as this?
    vb.net Code:
    1. Dim strString as string = "test"
    2. strString &= " this"

  14. #14

  15. #15

    Thread Starter
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Unique only data in string

    Both code solutions worked great. VBDT's had to have the statesStr declared but that wasn't a big issue.

    Thank you guys for your help, you're the best! I did you rate ya!

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