How to get value between _ and _

Hi all,

I have filepath names and I need to get the text that is between the first two underscores (for each file in the folder). For example cnaeow_elek_kdkek_ds.xlsx. How do I get that part of the string? How do I indicate that I need the substring between the _ and _? Thanks

Hi.

Just a quick solution where you can use the .Split function.
filename.Split("_"c)(1)

So it splits by the ā€œ_ā€ to create an array of values, then take the 2nd value with index of (1)

You can also use Regex if you do some searches for assistance with that.

Regards.

1 Like

@ClaytonM Thanks, I will give that a try. What is the ā€˜cā€™ for?

Hi @yannip,

ā€œcā€ indicating character.

Regards,
Arivu

1 Like

Hi @arivu96, @ClaytonM

Iā€™m getting an error:


image

Thanks

That error usually means there either was no ā€œ_ā€ character in the string to split by (and other times if you try to use an index that is too high).

Most of the time you will want to verify the character exists before splitting. You can do that with a simple condition in If activity or inline code.

For example:

If(WIName.ToString.Contains("_"), WIName.ToString.Split("_"c)(1), WIName.ToString)

The False side is up to you depending on what you need to do.

Regards.

Thanks, works as a charm.