Trying to use split to get the data prior to the split position

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.

1 Like

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?

1 Like

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.

This is a visual basic example of the same thing you might want to do for counting the number of character occurrences.

1 Like

image

You can extract this as a separate work flow like the image above if you want to clean it up a bit.

@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 :

String.Join("|",Split("|7575|Your|Name|HERE|38383|Assigned|2253|Your|Name|HERE|44583|Assigned|","|").Take(5))

4 Likes

Pretty Brilliant and elegant.

Would he still need to use an index check to account for every 5th pipe?

@cody.vollrath “Index Check” meaning to check if the Input String has 5 Pipes or not ? or did you mean Something else :sweat_smile:

1 Like

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”.

Sorry I am terrible at explaining myself lol.

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 :sweat_smile: that is essential.

1 Like

That pretty much sums up what I was asking. Thank you!

1 Like

@cssc, another option would be to take the Regex way.

First, it finds the index of the 5th pipe.
Then, it takes the substring until that index.

Give this a try:

inputStr.Substring(0, Regex.Matches(inputStr, "\|")(4).Index + 1)
1 Like

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