Reputation: 13
So i have a issue im running into and i cannot quite get my mind wrapped around it, hopefully i can explain this correctly.
I am working in VB.net
My string is
From the National Weather Service in Jackson: Severe Thunderstorm Warning [wind: 60 MPH, hail: 1.00 IN] for Pittsburgh [PA],Guernsey, Belmont [OH] till 3:45 PM CDT
So here is my issue, The counties listed above can be put in any order on each given warning/watch/advisory, So PA could be listed first or last or in the middle.
This is what i need, I need to be able to split the counties for PA into one section, OH in another, WV Into another and so on...
Example: Belmon, Guernsey, Noble [OH] Marshall, Ohio, Wetzel [WV]
Keeping in mind that the counties and states can change from warning to warning.
My overall goal is this, Its possible that Marion [OH] and Marion [WV] could be in the same warning, I want to leave Marion [WV] out and keep Marion [OH] along with any other counties that might be listed.
Dim string1 As String
string1 = Split(TextBox1.Text, "till")(0)
If InStr(TextBox1.Text, "[PA]") Then
string1 = Split(string1, "[PA]")(1)
string1 = Replace(string1, ", ", ",")
string1 = Replace(string1, ",", "|")
string1 = Split(string1, "[OH]")(0)
Else
If InStr(TextBox1.Text, "[WV]") Then
string1 = Split(string1, "[WV]")(1)
string1 = Replace(string1, ", ", ",")
string1 = Replace(string1, ",", "|")
string1 = Split(string1, "[OH]")(0)
End If
End If
TextBox2.Text = string1
Upvotes: 1
Views: 51
Reputation: 1264
It seems that you need to parse the string. I suggest you read about "Regular Expressions" (in short RegEx
). This is a method to find things in strings.
The RegEx
search pattern \[(PA|OH|CA)\]
for instance will give you any occurrence of the strings "[PA]" or "[OH]" or "[CA]" in a given source string.
Imports System.Text.RegularExpressions
Public Module Module1
Sub Main()
Dim source As String = "From the National Weather Service in Jackson: Severe Thunderstorm Warning [wind: 60 MPH, hail: 1.00 IN] for Pittsburgh [PA],Guernsey, Belmont [OH] till 3:45 PM CDT"
Dim searchPattern As String = "\[(PA|OH|CA)\]"
Dim matches As MatchCollection
matches = Regex.Matches(source, searchPattern)
For Each match As Match In matches
Console.WriteLine(String.Format("{0} at position {1} in source string", match.Value, match.Index))
Next match
Console.ReadKey()
End Sub
End Module
This short console program shows you how to use RegEx
in VB.NET.
Now you know where in your source string you can find state abbreviations, now you can parse the source string for the rest of the information needed.
Be sure to test your RegEx
search patterns thoroughly because usually they'll capture more or less than you may think. There are a lot of web sites to test RegEx
search patterns (e.g. https://regex101.com)
Upvotes: 1