This version of #PMonCall covers a more advanced Microsoft Flow concept, but one that a customer ran into and I figured it would be worth sharing with a broader audience.
In this scenario, our customer has a comma delimited string and they need to parse this string and access specific indexes within the array. For example we may have a string like “a,b,c,d” and the customer would like to access a specific index like ‘a’ or ‘b’.
To solve this problem, I performed the following steps:
- To simplify the solution, I am using a button as my trigger (it is my favorite trigger)
- To represent our comma delimited string, I will initialize a variable called myString and I will have a Value of a,b,c,d
- We need to be able to store the string that we are about to break apart. To do this we will initialize an array called myArray. For our Value we will use an expression that leverages the split function. For inputs to this function we need to provide our myString variable and our delimiter character which is a comma wrapped in single quotes ‘,’.
- The next requirement we need to deal with is accessing a specific index within our array. To demonstrate this we will create another variable called firstIndex and our Value will be an expression of variables(‘myArray’). The  in this statement represents the index which happens to be zero-based.Alternatively if we wanted to access our second value (‘b’) we could provide an expression of variables(‘myArray’). (Remember – zero based indexes)
Note: Naturally when accessing specific indexes, you are making assumptions on them existing. So I suggest adding checks for existence of values or using coalesce function to provide default value.
Let’s now run the flow to see how it behaves. During run time execution we can see that our comma delimited string has a value of “a,b,c,d” and that when we apply the split function, our array now has a properly assembled array.
- When we applied an index to the end of our array, through an expression of variables(‘myArray’), we can see that our variable has a value of ‘a’.
For developers, the previous scenario is something that you have likely encountered in the past when writing code. The good news is that you can solve this problem in flow, but it just takes learning about our expressions and understanding the model of passing parameters. Expressions can unlock a lot of additional use cases in Microsoft Flow, so I recommend learning more here.
3 thoughts on “PM on Call: Split Strings and Access Array Indexes”
Nice. I like Split function to make a array.
and apply for each it, filter, filter, and join.
Thank you so much for this post.
I searched for a good day trying to find a simple method to do this and all of the others seemed to be more involved with JSON, adding extra difficulty into accomplishing this task.
You saved my day.