How to exclude the middle name (alphabet) and split the full name as 'First Name' and 'Last Name' on separate columns

Hello there,

I am able to split the first name and last name into different columns from a full name. However, in my project in some cases some names have middle name (alphabet). How can I exclude middle name (alphabet) dynamically as I have thousands of names in my names’ list? Thanks
Please see the following screenshot for your visibility. Thanks!

Hi,

Can you try the following?

dt.AsEnumerable.ToList.ForEach(
Sub(r)
    r("FirstName")=System.Text.RegularExpressions.Regex.Match(r("FullName").ToString,"^\S+").Value
	r("LastName")=System.Text.RegularExpressions.Regex.Match(r("FullName").ToString,"\S+$").Value
End Sub
)

Sample20221122-1aL.zip (3.0 KB)

Regards,

Thanks a lot, @Yoichi. Yes, the split works for all the scenarios, including the full names with(out) middle name (alphabet). I have a follow up question for you, if you do not mind.
– I do not want to see the column called fullname in the output (result) file, just first name and last name, just split ones. How do I restrict the fullname column from going to output?
– I need all the columns in the input except the fullname in my output (result) file
– Not very much essential but just FYI, I use modern design in my process.
Please advise. Thanks!

Hi,

We can use RemoveDataColumn Activity as the following.

image

FYI, if you need to change column position of FirstName and/or LastName, the following will help you.

dt.Columns("FirstName")SetOrdinal(0)

Regards,

Hi @Yoichi

This is your process, just added remove column activity and it works fine (meaning I can see only the first_name and last name)

However, in my case
I need to use an input excel file that has fullname
and after the data manipulation is sent into another excel file (output). Tried a couple of times…
and it looks like I am missing something important. Please have a look at this screen shot

image.png

In the output excel file everything is the same, except the names are split as firstName and LastName

Thank you a lot for your help!

Employee_New.xlsx (11 KB)

Hi,

If you need to write it to another excel workbook, it’s necessary to add UseExcelFile activity as the following. Can you try this?

Regards,

I got this error

But able to load the data to another excel file.

I wonder where I can see the property you were trying to explain

“FYI, if you need to change column position of FirstName and/or LastName, the following will help you.”

Thanks!

HI,

This error shows the DataTable already has “FirstName” column. Do you read it from Excel sheet? is there already FirstName column? if so, it’s unnecessary to call this activity.

If it’s uncertain the column exists or not, please use If activity to check in advance, as the following.

image

Regards,

@Yoichi thanks!
I did the same thing (If) for both First name and Last name and I received the following error for the next activity (Invoke Code). FYI- regardless of the thrown error I can see data loaded into the output file. This issue happens when I use “Remove Data Column” after “Invoke code” activity. One more thing, I was trying to rearrange the location of First Name and Last name to be located in the first and second columns respectively. Thanks!

image (17)

HI,

Can you share content of $exceptionDetail at Locals panel when error occurs in debug mode?

Regards,

HI,

Can you share whole message as text? Probably we need the first 2 or 3 lines.
(we need text after “Exception has been thrown by…” at the first line in the above image)

Regards,

======
22.4.4+Branch.support-v22.4.Sha.1090422ff8ac244a830d9e33650f90eb9cddd56a

Source: Invoke Code

Message: Exception has been thrown by the target of an invocation.

Exception Type: System.Reflection.TargetInvocationException

RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> RemoteException wrapping System.ArgumentException: Column ‘FullName’ does not belong to table A1.
at System.Data.DataRow.GetDataColumn(String columnName)
at System.Data.DataRow.get_Item(String columnName)
at UiPathCodeRunner_23e9a0dbe45a4446b0817fe83373eaaf._Closure$__._Lambda$__0-0(DataRow r)
at System.Collections.Generic.List1.ForEach(Action1 action)
at UiPathCodeRunner_23e9a0dbe45a4446b0817fe83373eaaf.Run(DataTable& dt)
— End of inner exception stack trace —
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object providedArgs, ParameterModifier modifiers, CultureInfo culture, String namedParams)
at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object args)
at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List1 inArgs, IEnumerable1 imps, Object args)
at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

=====

Hi,

Error message says there is no column named “FullName”. Did you input datatable which is already deleted the column?

Regards,

ErrorMessage.txt (2.1 KB)

I added after the invoke code activity

Hi,

Can you check content of your datatable at LocalsPanel when error occurs in debug mode?

Regards,

here is the error I see