cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Split string into array of characters - empty delimiter not working

Trying to split a string into an array of characters, but using a delimiter of nothing '<empty>' does not work.  Any help would be appreciated.

 

Simple variable creation to test it out... 

 

Expression with empty delimiter:

array(split('testing',''))

Result:

[
  "testing"
]

With a delimiter:

array(split('testing','t'))

Result: (expected)

[
  "",
  "es",
  "ing"
]

 

Thanks in advance, and sorry if it is something simple I missed along the way.

1 ACCEPTED SOLUTION

Accepted Solutions
Anonymous
Not applicable

Everyone - I have an actual solution to this problem.

The problem, as a few repliers seem to have missed, is splitting a string into an array of characters. So, for instance, if we had the string "ABCD", we want the array <code?["A", "B", "C", "D"]. We're not trying to split on spaces, or commas, or any other delimiter - we want the individual characters in the string.

The solution is to use the Select action with the range() and substring() functions. If you didn't know, Select is kind of like a map function - it produces a given output for each element of the input. range() creates an array from a given range of numbers. substring() creates new strings from a selected range of a given string. Together, we have all the ingredients we need.

The flow overall.The flow overall.

The secret sauce here is the little icon in the bottom right of the Select action - that switches it from the default behaviour mapping an array to an array of dictionaries, to the far more useful behaviour of mapping an array to an array of… well, anything. The icon looks like a little T in a box.

Here's the expressions we want, assuming the string is in a variable called StringToSplit:

From:

range(0, length(variables('StringToSplit')))

Map:

substring(variables('StringToSplit'), item(), 1)

Let's say we have the string "Hello, world!". The first expression creates an array of integers, starting at 0, up to the length of the string (13 characters). So we get the input array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]. The map expression is then applied to each of these. The first iteration, we have the input item 0, so the map expression becomes substring('Hello, world!', 0, 1) - in other words, give us a substring from the start of the string, that's one character long. So, we get 'H'. The second iteration, the map expression becomes substring('Hello, world!', 1, 1) - give us a substring starting one character from the start, one character long. That's 'e'. And so on until we get to the last element, which becomes substring('Hello, world!', 12, 1) - skipping the first twelve characters and giving us the next one, which is '!'.

The result: ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]

I created a gist with a short demo, in Logic Apps and Power Automate flavours. You can check it out here: https://gist.github.com/gormster/acbdc30bffab6bd54e45e02605a672d5

 

PS: Why can't we use <code> tags in this forum? You have CSS for it and everything, but it gets stripped out when I click Post. Seems crazy to me.

View solution in original post

10 REPLIES 10
v-bacao-msft
Community Support
Community Support

Hi @Anonymous,

 

I did a test on my side, and the result of the expression output you provided is what you expected.

I am a bit confused about what kind of data you need to get.

65.PNG66.PNG

Best Regards,

Barry

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Ok, so you tested my second case, which was as expected.

 

The case I am trying to solve is with an empty delimiter.

 

So you would use: Split('testing','')

And you should recieve: ["t","e","s","t","i","n","g"]

 

split issue - input.PNGsplit issue - result.PNG

Hi @Anonymous,

 

I tested it on my side, and relying on the split() function doesn't seem to get the results you expected. Because such a separator seems to have no effect.

You could try other methods. I used variables and expressions on my side to get the results you want, maybe you could refer to.

74.PNG75.PNG

Best Regards,

Barry

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi @Anonymous,

 

Have you tried the method I provided?

It seems that using the Split() function as you described does not produce the expected results.

If you need further assistance, please feel free let me know.

 

Best Regards,

Barry

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Sorry for the delay, holidays you know! I have not tried the method you outlined. Mostly, as it is gigantic, and I feel like it would completely overbear the point of the flow. While my example used the word "testing" it is likely that these will be phrases for our content (e.g. "Now is the time for all good men to come to the aid of their country"). I was hoping to use this process to check for/replace reserved characters and words for SharePoint as we use these phrases to generate unique locations. I was really hoping to find something simple an elegant, where the error handling didn't dwarf the entire flow. Any chance we can log this somewhere w/ MSFT to get the split function fixed? It works correctly in every other product offering...

Hi Wdenny,

I had the same issue. I worked around this limitation as follows:

Define a String variable called Subject (My initial variable was a string that was taken from the subject line in an email.)

Define a second string variable called CommaSubject, set its value to replace each space with a comma i.e. replace(triggerBody()?['Subject'],' ',',')     Although it may be better to use a less common character such as ^ for the separator.

You can then split that string using the character you replaced.

 

Cristian

 

I can see that @v-bacao-msft 's reply counts as a solution, but it's very much convoluted.

Splitting strings with an empty delimiter is pervasive among programming language frameworks and should work as OP intended.

Anonymous
Not applicable

The answer is simple. You have to put a whitespace bettwen the single quotes as the separator, just like that: ' '. You can not wrote any "" or null inside the two single quotes, cause json does not reconize that. Bellow you can see an example:  

 

split(item()?['User']?['DisplayName'],' ')

Anonymous
Not applicable

Everyone - I have an actual solution to this problem.

The problem, as a few repliers seem to have missed, is splitting a string into an array of characters. So, for instance, if we had the string "ABCD", we want the array <code?["A", "B", "C", "D"]. We're not trying to split on spaces, or commas, or any other delimiter - we want the individual characters in the string.

The solution is to use the Select action with the range() and substring() functions. If you didn't know, Select is kind of like a map function - it produces a given output for each element of the input. range() creates an array from a given range of numbers. substring() creates new strings from a selected range of a given string. Together, we have all the ingredients we need.

The flow overall.The flow overall.

The secret sauce here is the little icon in the bottom right of the Select action - that switches it from the default behaviour mapping an array to an array of dictionaries, to the far more useful behaviour of mapping an array to an array of… well, anything. The icon looks like a little T in a box.

Here's the expressions we want, assuming the string is in a variable called StringToSplit:

From:

range(0, length(variables('StringToSplit')))

Map:

substring(variables('StringToSplit'), item(), 1)

Let's say we have the string "Hello, world!". The first expression creates an array of integers, starting at 0, up to the length of the string (13 characters). So we get the input array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]. The map expression is then applied to each of these. The first iteration, we have the input item 0, so the map expression becomes substring('Hello, world!', 0, 1) - in other words, give us a substring from the start of the string, that's one character long. So, we get 'H'. The second iteration, the map expression becomes substring('Hello, world!', 1, 1) - give us a substring starting one character from the start, one character long. That's 'e'. And so on until we get to the last element, which becomes substring('Hello, world!', 12, 1) - skipping the first twelve characters and giving us the next one, which is '!'.

The result: ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]

I created a gist with a short demo, in Logic Apps and Power Automate flavours. You can check it out here: https://gist.github.com/gormster/acbdc30bffab6bd54e45e02605a672d5

 

PS: Why can't we use <code> tags in this forum? You have CSS for it and everything, but it gets stripped out when I click Post. Seems crazy to me.

Glad I read to the bottom...any way to mark this as the real solution?!

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

New Ideas Forum MPA.jpg

A new place to submit your Ideas for Power Automate

Announcing a new way to share your feedback with the Power Automate Team.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

MPA Licensing.jpg

Ask your licensing questions at the Power Automate AMA!

Join Priya Kodukula and the licensing team, super users and MVPs to find answers to your questions on Power Automate licensing.

Users online (1,523)