Creating a datatable from a string with spaces as delimiters

Hey there,
I’m looking for a way to convert a string, with spaces as delimiters, into a data table that has 10 columns.
Another problem with that is that there are bunch of spaces between each item.

1 Like

Is it fixed width in that each column text begins at the same position?

1 Like

No, most of them are, but some columns have numbers. If it is aa shorter number, then there are more spaces, and the other way around.

You can pull them apart using the split function and build your data row:

  1. Read text into TableName
  2. For Each Row in TableName
  3. Assign NewDataRow = TableName.NewRow
  4. Assign NewDataRow(0) = Trim(String.Split(" "c)(0).ToStrnig)
  5. Assigned NewDataRow(1) = Trim(String.Split(" "c)(1).ToString) and so on…
    Finally, Use Add Data Row activity (set Data Row as NewDataRow and table = TableName)

Does that make sense?

1 Like

Thanks for the replies, I’ll try it out in a sec.

1 Like

No problem. This assumes none of the data has spaces in it. If it does, you would need to try and identify a number of spaces that separate them as a delimiter instead. For instance, String.Split(" ",c) would make 5 spaces be your delimiter. The trim removes any additional spaces.

For some reason, UiPath is removing my 5 spaces above and making it appear as just one. I meant for it to show 5 spaces in between the quotes.

1 Like

Ok, something doesn’t work out for me so I’ll check if I understood you correctly.
I have a 1 dimentional string - 1 line that looks something like this:

ababab ababbababba ababbabababababba aabbababab abbababba.

First I have to split it. I do it like this: Assign string_dt_array = Split(string_dt,“ ”)
Then read text into the dtable. I don’t get this part.
what does it mean to read text into it?

Hi,

Have you tried to use the “Generate Data Table” Activity?

https://activities.uipath.com/docs/generate-data-table

If you know the Columns in advance, I would recommend specifying the ColumnSizes properties.

For your extra spaces, are you sure those aren’t “tabs” (see below)?

If not, you could replace “more than one space” by “One space”. You can do it with the Replace Activity with the Pattern “\s+” ==> “”

Cheers

6 Likes

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