How to Trim Data Column Values, from 0 to 50

I’m trying to Limit/Trim a data column from zero to 50, without using For each loop. I’m using an Invoke Code with this:

dt.AsEnumerable().ToList.ForEach(Sub(row) row(0)=row(0).ToString.SubString(0,50))

Where it would substring the datas to 5. Problem is most of rows that are either empty or is less than 50. Is there a way to trim it without using for each loop?

@Shinjid

Try below expression.

       dt.AsEnumerable().ToList.ForEach(Sub(row) If(row(0).ToString.Trim.Length > 50,row(0).ToString.Trim.SubString(0,50),row(0).ToString.Trim))

Invoke code: No compiled code to run
error BC30198: ‘)’ expected. At line 1
I’ve added another ‘)’ in the end, still the same

@Shinjid

Did you try the above expression ?

Except for the missing parenthesis, you need an assign statement also, since you are using a Sub().

dt.AsEnumerable().ToList.ForEach(Sub(row) row(0) = If(row(0).ToString.Trim.Length > 50, row(0).ToString.Trim.SubString(0,50), row(0).ToString.Trim))

@Shinjid Try with the attached workflow. The code is in a notepad, we can call using invoke VBA. Just pass the range from UiPath

Example.zip (10.9 KB)

HI,

FYI, Another solution:

If you use VB.net in Invoke code, the following will work.

dt.AsEnumerable().ToList.ForEach(Sub(row) row(0)=Left(row(0).ToString(),50))

Regards,

1 Like

You mean put the code to an assign activity? I tried this before, but it gives “Expression does not produce a value” I had similar code but with string replace instead, and it only works on mine if I put it in Invoke Code.

Just use the expression I provided. I have corrected it for you.

dt.AsEnumerable().ToList.ForEach(Sub(row) row(0) = If(row(0).ToString.Trim.Length > 50, row(0).ToString.Trim.SubString(0,50), row(0).ToString.Trim))
1 Like

Thanks! This works now in Invoke Code.

1 Like

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