How to remove leading and trailing spaces from all cells in DataTable?

I want to remove all leading and trailing spaces from ALL cells in a datatable.

I have an idea where I can probably use nested for-each loop, but I am not sure exactly how to do this. I first thought about something like:

for each column in dt_Example.Columns:
__for each row in column
____Assign: dt_Example.Rows(row, column).ToString = dt_Example.Rows(row, column).ToString.Trim

but this is kind of too much work, and I don’t even think this works. Some cells have integer values too, so I am not sure if I can just put ToString.

Is there any smart way to remove all spaces from front and back of data in ALL cells in a data table?

Hmmm: I am not sure what I am doing wrong. Can anyone help me, please?
Trial 1: (in_dt_WithSpaces is an argument of type DataTable from which I want to get rid of all leading and trailing spaces from all cells.)
1

Trial 2 (Pseudo code):
i (of Int32, default set to 0 from variable tab)
j (of Int32, default set to 0 from variable tab)
while ( i < in_dt_withSpaces.Rows.Count ){
while ( j < in_dt_WithSpaces.Columns.Count ){
Assign: in_dt_WithSpaces.Rows(i)(j) = in_dt_WithSpaces.Rows(i)(j).ToString.Trim
Assign: j = j + 1
…}
Assign: i = i + 1
}

I tried the above code, but it doesn’t work. I am at my wits end…

Hi @tomato25

Try the below post

How to use the trim, split, substring, remove

Thanks
Ashwin.S

1 Like

Hi @tomato25,

I have attached the below xaml for your reference.
check if it meets your requirements.
Excel.zip (21.5 KB)

1 Like

wow, I didnt know it was this simple.

but I have one problem. I actually do not know the column names, or even if I knew, I cannot hard code the column names, and there are many columns too.

Any way to do this without knowing the names of the columns?

1 Like

@tomato25

You can do it using Column Index also.

DataTable.Item(0) - first column

DataTable.Item(1) - Second column

Etc…

1 Like

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