VB.NET: How to load the following values from a string into a collection
i have a string = "/a/value1/x/valueforx开发者_Python百科/b/value2/c/value3"
the keys a, b, and c can be in any order in the string that is coming in and will always be separated by a slash.
What i need to do is to find a, b, c
and then add their values to a collection
Use the string.Split function.
Then iterate through the results and pick up each that you want to retrieve.
E.g. something like (C#):
var splitted = "/a/value1/x/valueforx/b/value2/c/value3".Split('/');
for ( var index = 0; index<splitted.Length; ++index )
{
if ( s=="a" )
{
var valueOfA = s[index+1];
// Process valueOfA.
}
// Same for "b" and "c".
}
The same in VB.NET:
Dim splitted = "/a/value1/x/valueforx/b/value2/c/value3".Split("/"C)
For index As Integer = 0 To splitted.Length - 1
If s = "a" Then
' Process valueOfA.
Dim valueOfA = s(index + 1)
' Same for "b" and "c".
End If
Next
I omitted error checking here (i.e. array bounds checks)
Like Uwe Keim said, use String.Split
. What I would do is make a quick function that returns a dictionary.
This code is untested, but should get you started...
Public Function GetConfigFromMagicString(ByVal sSource as string) as Dictionary(of String, string)
Dim ConfigDict as Dictionary (of String, String)
Dim Pieces as string() = sSource.split("/")
For x as Integer = 0 to Pieces.Length-1 Step 2
ConfigDict.add(pieces(x), pieces(x+1)
Next x
return ConfigDict
End Function
From there, you just retrieve it like this...
Dim myConfig as Dictionary (of string, string) = getConfigFromMagicString("a/value1/x/valueforx/b/value2/c/value3")
msgbox(myConfig.Item("a"))
This should cover the basics of the algorithm. It'll need a little cleaning to make sure that there aren't any unexpected conditions that may throw exceptions.
Dim myDict as Dictionary(Of String, String) = New Dictionary(Of String, String)()
Dim myStr as String = "/a/value1/b/value2/c/value3"
Dim temp() As String = myStr.Split("/"c)
For i As Integer = 0 to temp.Length Step 2
'Assumes that it will always be an array length divisible by
'2. If this is not the case, logic is necessary to prevent
'for loop from bombing at the end of the sequence
'EDIT: for clarification
Select Case temp(i)
Case "a", "b", "c"
myDict(temp(i)) = temp(i + 1)
DefaultCase
Continue
End Select
Next
string s = "/a/value1/b/value2/c/value3";
List<KeyValuePair<string, string>> kvList = new List<KeyValuePair<string, string>>();
string[] strList = s.Split(new char[]{'/'} , StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i <= strList.Length - 2 ; i += 2)
{
KeyValuePair<string, string> keyValue =
new KeyValuePair<string, string> (strList[i] , strList[i + 1]);
kvList.Add(keyValue);
}
it is C#..
Add this to the top of your code:
Imports System.Text.RegularExpressions
Then use this:
Dim mc As MatchCollection = Regex.Matches("/a/value1/x/valueforx/b/value2/c/value3", "(?<=/[abc]/)[a-zA-Z0-9]*")
To put these matches into a string collection as requested you can just add this after the above code:
Dim sc As New Collections.Specialized.StringCollection
For Each m As Match In mc
sc.Add(m.Value)
Next
Using your input this produces matches with values of
- value1
- value2
- value3
精彩评论