How to trim a particular column of a datatable

Hello,

I want to trim all the rows of a specific column using linq. Please help. Thanks!

@riyatona18
Ok
To trim a particular column of a DataTable in UiPath, you can use the For Each Row activity combined with the Assign activity to update the values in the desired column. Here’s an example of how you can trim a specific column:

  1. Use the For Each Row activity to iterate through each row of the DataTable.
  2. Inside the loop, add an Assign activity to update the value of the desired column.

item(“ColumnName”) = item(“ColumnName”).ToString.trim

  1. Use the Trim function to remove any leading or trailing spaces from the column value.
  2. Assign the trimmed value back to the same column.

Hi Raja,

Row value is having extra space and I want to trim that. I don’t want to delete the column.

For eg:

Col1 Col2
Row1 Row1
Row2 Row2

I want to trim the space before Row2 value.

Hi @riyatona18 ,

Use invoke code activity and pass dt as in/out argument:

dt.AsEnumerable.ToList.ForEach(Sub(x) x("ColumnName") = x("ColumnName").ToString.trim)

Regards,

1 Like

Hi Vishal,

Thanks for your quick response. Invoke code activity is mandatory?

I want to use assign and the output should be added to the same DT.
Thanks!

@riyatona18 : For Invoke Code activity, You need to pass argument with name dt & map it to your Datatable & select datatype as “Datatable” & direction as “In/Out” it will work.

Hi @riyatona18

Try this-

Example-

var data = new
{
new { Name = " John ", Age = 25 },
new { Name = " Alice ", Age = 30 },
new { Name = " Bob ", Age = 35 }
};

var trimmedData = data.Select(row => new
{
Name = row.Name.Trim(),
row.Age
}).ToList();

    foreach (var row in trimmedData)
    {
        Console.WriteLine($"Name: '{row.Name}', Age: {row.Age}");
    }

Thanks!!