Split String Based on Delimiter

Hello! I have a text file that I need to read, line by line, and extract the values based on a colon (“:”) delimiter. The file is structured in the following way:

Category_1 : Value_1
Category_2 : Value_2
Category_3 : Value_3

Category_n : Value_n

I am trying to automate the provisioning process for an application in my company, so I need to be able to extract these values and enter them into a form. If I can simply extract the values themselves, then that will be sufficient. However, I’d prefer to have them assigned to variables named after the category. This would make it more intuitive to work into the rest of my workflow.

Ex: variable : category_1 => value: value_1

Help would be appreciated. Thanks in advance.

@ibarnes
As it is dynamic you can not do on runtime as variables have to be defined before compiling. But you can use a dictionary (Of String, String) and fill it up dynamicly on runtime with dictVar(“Category_X”) = Value_X

1 Like

1.) Create a variable MyLineArr of type String Array.

2.) If MyText is the text you’ve read from the file, set MyLineArr to MyText.Split("\n"c).

3.) Create a For Eacch loop to iterate over the elements of MyLineArr.

4.) In the For Each loop, if MyLine is an element of MyLineArr, you can get the category from System.Text.RegularExpressions.Regex.Split(MyLine, "\s*:\s*")(0), and the value from System.Text.RegularExpressions.Regex.Split(MyLine, "\s*:\s*")(1).

1 Like

Hello Anthony,

I don’t see the String Array option under Variable type. Is there a certain package I need to download?

Thanks

Choose the Array option, and UiPath will ask you what type you want to use. Choose String from the popup window.

Okay, I’ve done that, but when I set that variable equal to what you told me to, I get the error: "uipath option strict on disallows implicit conversions from ‘string’ to ‘char’.

Use MyText.Split("\n"c) instead.

Now it gives me this error:

“uipath character constant must contain exactly one character”

Hmm, try this then:

MyText.Split(Environment.NewLine).

Gives me the error from before now:

“strict on disallows implicit conversions from ‘string’ to ‘char’”

MyText.Split(New String() {Environment.NewLine},StringSplitOptions.None)

Okay that removed that error. Do I need to use an assign statement within the for each activity?

Yes. You’ll be getting each category and value in the For Each loop using Assign.

Okay, going back to the final step you sent, what do you mean by “if MyLine is an element of MyLineArr”? Should I set “MyLine” equal to one of the two regex statements you sent (for the category and value)?

Here is what my interface looks like

MyLine is item in your screenshot. I just used MyLine to specify what was being pulled from the array, as item represents a single line in the data you’ve split.

Okay, so do I just need to set the first regex statement equal to the second one?

Ex: System.Text.RegularExpressions.Regex.Split(MyLine, “\s*:\s*”)(0) = System.Text.RegularExpressions.Regex.Split(MyLine, “\s*:\s*”)(1)

No, you’ll need to assign variables (e.g. Category and Value) to System.Text.RegularExpressions.Regex.Split(MyLine, “\s*:\s*”)(0) and System.Text.RegularExpressions.Regex.Split(MyLine, “\s*:\s*”)(1).

Alright, I’ve tried this for Category, but I’m getting the “strict on disallows implicit conversions from ‘object’ to ‘string’” error. Here’s what my setup looks like:

Also, since this is a “For Each”, wouldn’t the current Category and Value variables get overwritten by the next iteration when it runs? Ideally, I’d like to end up with a number of different variables (named after the category) whose values are those from the split text.

Oh, append .ToString to the end of the values.

And yes, they would be overwritten, so you need to handle these variables in this loop however you intend to deal with them, whether processing them at this moment, adding to a dictionary, adding to a datatable, etc.

Append like this?

System.Text.RegularExpressions.Regex.Split(MyLine, “\s*:\s*”)(0).ToString

or like this?

System.Text.RegularExpressions.Regex.Split.ToString(MyLine, “\s*:\s*”)(0)

The first way still gives me the same error. The second way gives me this error:

“Overload resolution failed because no accessible ‘Split’ accepts this number of arguments.”