My goal is to make the string ...
vb Code:
strLocations = "WI - WI - WI - IL - IL"
to
vb Code:
strLocations = "WI - IL"
I'm restricted to using the 2.0 framework as well.
Any ideas or help would be greatly appreciated.
Printable View
My goal is to make the string ...
vb Code:
strLocations = "WI - WI - WI - IL - IL"
to
vb Code:
strLocations = "WI - IL"
I'm restricted to using the 2.0 framework as well.
Any ideas or help would be greatly appreciated.
Is this a homework assignment? I'm just curious.
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"
@Zeljko: Is the data really going to be the same each time???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)
+ The class is STRING, not STRINGS.
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.
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.
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
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
Did everyone just ignore my post?
I didn't know the Of keyword was available in 2.0. Anyways, if you have it, replace ArrayList with List(Of 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)
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!
&= is shorthand for ... = ... & ... .
So you can append a string to another with shorthand.
Yes.
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!