How to build a Dictionary from a String Variable

I have a strint that i want build a dictionary variable.

Item1 : Result1
Item2 : Result2
Item3 : Result3
…and so on

This is basicaly the structure from it. I want to build the dictionary variable to display the result based on the selected item.

Like: DictionaryVariable(“Item2”).ToString
Result: Result2

I don’t know how to input the string into the dictionary and use the colon to split the key and value.

maybe you are looking for this:

Create a variable: dictItems

assign Activity
dictItems = new Dictionary(Of String, String)

add following string Item1 : Result1 - str1

Assign Activity:
dictItems(str1.Split(":“c)(0).Trim) = str1.Split(”:"c)(1).Trim

Thats no good unfortunately. This way, i would have to do the split on every row of my string, and the row number is variable too. I need to input the entire string at once.

then it would be better that you are describing your case more exact

unlcear what is meant in detail

just give us some sample strings and expected resulting dictionary

@MaysonLedur How about this

Refer below workflow (2.8 KB)

In general we would more focus on a strategy like:

  • bring the values into a collection - e.g. Array, List…
  • create the dictionary from this (e.g. LINQ, Loops)
  • enrich / Split / generate the Keys


where in your case it could also be like:

arrItems = {strVar1, strVar2 …}
arrItems.Select(Function (x) x.Split(":"c).Select(function (s) s.Trim)).ToDictionary(Function (a) a(0), Function (a) a(1))

My imput string could be like:

Item1 : Result1
Item2 : Result2
Item3 : Result3


Item4 : Ball
Item5 : 12345
Item6 : Car
Item7 : I’m lost at this

The only constancy is a String on left, " : " in between and another String on Right

most of the working working blocks are demonstrated as above

here it can be more exact.

But lets assume you string is e.g. content from a txt file / get text… and has line breaks

then the same as described above

just to mention one of several options

1 Like

Hi @MaysonLedur,

As an alternative to the approach @ppr suggested.

The below approach will also take care of both your fixed pattern and is independent of the number of rows in your string.

  1. We split at new line, the output array consist of fixed patterns (left:right) which we can extract by another split as shown.

  2. Finally the add method adds the key values to a output dictionary.

Here is the .xaml file : StringToDictionary.xaml (11.1 KB)

Hope this helps.

1 Like

Here is my soluction.
strIntoDictionary.xaml (10.0 KB)

1st: Convert the string into an array using “new line” as separator
2nd: Declair the Dictionary variable
3rd: For Each item on the Array and “Add to Dictionary” activitie (from Microsoft.Activities.Extensions) where the
Key: Split(item," : “)(0) and Value: Split(item,” : ")(1)

Hope i can help someone else!

You can also do away with installation of additional libraries if you use the Invoke Method activity (as shown above).

1 Like

Nice! I’ll try too

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.