1

How to split the string based on white spaces in DataFlow expression builder in ADF pipeline. A string can have 1 or more white spaces.

Eg: 

I used split(name,’ ‘) : Thiswill split the word based on single space.Some times it can have more than 1 space. How to handle that?

Joe Smith(1 white space)
Joel  Smith(2 white space)
Joplin   Smith(3 white space)
Jen
  • 87
  • 7
  • Can you please add what you have tried and error if facing any? – Pratik Lad Apr 11 '23 at 15:05
  • @Pratik Lad - I used split(name,’ ‘) : Thiswill split the word based on single space.Some times it can have more than 1 space. How to handle that – Jen Apr 12 '23 at 00:11

2 Answers2

0

This is my sample data with spaces.

enter image description here

column
Steve Smith C      S  K
Rakesh Govindula     Laddu
Chinna R     C      B

Use the below expression to get the words array without empty strings.

filter(split(column,' '),#item!='')

enter image description here

As there is no join function in dataflow expressions, to get the string of words with one space, use below expression from this SO Answer by @Jarred Jobe.

dropLeft(toString(reduce(filter(split(column,' '),#item!=''), '', #acc +  ' '  + #item, #result)), 0)

enter image description here

Rakesh Govindula
  • 5,257
  • 1
  • 2
  • 11
0

I notice a derivative of this problem from other programming langauges. If you want to remove multiple extra spaces then you can use 3 Replace() functions in a row to replace them with a single space.

If we set a parameter "newparam" to your example "Joplin Smith", we can use 3 replaces like so:

@replace(replace(replace(pipeline().parameters.newparam, ' ', ' %'), '% ', ''), '%', '')

Output after each replace:

 1 - "value": "Joplin % % %Smith"
 2 - "value": "Joplin %Smith"
 3 - "value": "Joplin Smith"

At this point you can wrap the command with the split() function and it will only intercept single spaces:

@split(replace(replace(replace(pipeline().parameters.newparam, ' ', ' %'), '% ', ''), '%', '')
, ' ')

Produces the array:

"value": [
        "Joplin",
        "Smith"
    ]
Zorkolot
  • 1,899
  • 1
  • 11
  • 8