Hey people, I have the following text “|7575|Your|Name|HERE|38383|Assigned|” and I wanted to assign everything prior to the last pipe character to a new variable.
The catch is that the string I have will be different every time and may include multiple lines, for example “|7575|Your|Name|HERE|38383|Assigned|2253|Your|Name|HERE|44583|Assigned|” and so I need to always extract the data prior to the fifth pipe character.
I know how to split the string so I get everything after the pipe character, just not before.
So you have a tricky problem but nothing that cant fixed with a simple for loop, index value, and 2 if statements.
What you need to do is define the initial string, and split it and give that to a Array of type string value.
Then, create an index variable of type int32 and set it to 0 initially.
After that, iterate through the split string with a for loop and set the type to String so that item is a string.
In side the for each loop, test if the index is equal to 6 and place another if statement in the else condition. In the if statement within in the else condition, test if index is less than or equal to 4 and perform your logic with that element. (Since you want all elements before the 5th pipe)
In the Then condition of the if statement that checks for index being equal to 6, set the index to 0.
After this, set index equal to index + 1.
Here is a screen shot of the whole workflow I created:
Hey Cody, thanks. I feel like there’s gotta be a simpler way to do this via just one or two assign functions? Is there no ‘function(x)’ type code I could use to count the pipe characters, or a count of the pipe characters then a split inside of a reverse function splitting at the total of the pipe characters - 5 and then a another reverse wrapped around it or something like that?
You could count the pipe characters instead of the element, though that requires to iterate through the string. I suggest doing what I did and separating it to a different workflow and invoking it in the for loop. The time complexity will pretty much be the same as what you want to do.
@cssc May I know from this above data what is the value you want to Extract? Is it all data prior to the 5th Pipe Character or only one word before it ?
If it’s all data prior to the 5th Pipe Character then Try the Below :
No, not to check if the string has 5 pipes, though that could be a factor for validation. I mean, assuming that the string has all of the fields delimited, with multiple records for example: “|7575|Your|Name|HERE|38383|Assigned|71235|Second|Name|HERE|13472|Assigned|”.
Would he still need to iterate and check the index so that he gets every instance of the first 5 pipes before the record starts over?
In other words, he would get “7575|Your|Name|HERE”, and then he would get “71235|Second|Name|HERE”.
To be able to get this kind of output again we would have to enhance the Solution with a While Loop and and by setting a Counter, and then use Skip method along with the Take as far as my understanding goes that is essential.