Take the 2-minute tour ×

i have a string = "/a/value1/x/valueforx/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

share|improve this question

5 Answers 5

up vote 0 down vote accepted

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")
share|improve this answer

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

I omitted error checking here (i.e. array bounds checks)

share|improve this answer
there could be other values that i don't want such as g/value –  asf Jan 14 '11 at 19:21
and i only want to add the three values of a, b, c into the collection in that order –  asf Jan 14 '11 at 19:22
i guess my question is how to do that –  asf Jan 14 '11 at 19:24
@Uwe Keim - That's some very strange looking vb.net code. –  Joel Etherton Jan 14 '11 at 19:29
@Joel Yes, that's how we write VB.NET here over in Germany. –  Uwe Keim Jan 14 '11 at 19:31

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)

    End Select
share|improve this answer
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]);


it is C#..

share|improve this answer

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

Using your input this produces matches with values of

  • value1
  • value2
  • value3
share|improve this answer

Your Answer


