|
-
May 4th, 2010, 04:52 PM
#1
Thread Starter
Addicted Member
Unique only data in string
My goal is to make the string ...
vb Code:
strLocations = "WI - WI - WI - IL - IL"
to
I'm restricted to using the 2.0 framework as well.
Any ideas or help would be greatly appreciated.
-
May 4th, 2010, 05:45 PM
#2
Re: Unique only data in string
Is this a homework assignment? I'm just curious.
-
May 4th, 2010, 07:10 PM
#3
Hyperactive Member
Re: Unique only data in string
VB.Net Code:
Dim strLocations As String = "WI - WI - WI - IL - IL"
strLocations = Strings.Left(strLocations, 2) & " - " & Strings.Right(strLocations, 2)
MsgBox(strLocations) 'Result in MsgBox is "WI - IL"
-
May 4th, 2010, 07:11 PM
#4
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.
-
May 4th, 2010, 07:59 PM
#5
Thread Starter
Addicted Member
Re: Unique only data in string
 Originally Posted by formlesstree4
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.
-
May 4th, 2010, 08:48 PM
#6
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.
-
May 4th, 2010, 08:50 PM
#7
Thread Starter
Addicted Member
Re: Unique only data in string
 Originally Posted by VBDT
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.
-
May 4th, 2010, 09:03 PM
#8
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
-
May 4th, 2010, 09:08 PM
#9
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
-
May 4th, 2010, 09:08 PM
#10
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.
-
May 4th, 2010, 09:27 PM
#11
Thread Starter
Addicted Member
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:
newStatesStr &= " - " & stateList(i)
Thanks for all of the suggestions guys!
-
May 4th, 2010, 09:43 PM
#12
Re: Unique only data in string
&= is shorthand for ... = ... & ... .
So you can append a string to another with shorthand.
-
May 4th, 2010, 10:16 PM
#13
Thread Starter
Addicted Member
Re: Unique only data in string
 Originally Posted by minitech
&= is shorthand for ... = ... & ... .
So you can append a string to another with shorthand.
So,
vb.net Code:
Dim strString as string = "test"
strString = strString & " this"
is the same as this?
vb.net Code:
Dim strString as string = "test"
strString &= " this"
-
May 4th, 2010, 10:32 PM
#14
Re: Unique only data in string
-
May 5th, 2010, 07:09 AM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|