How to Split first name and lastname dynamically

Hi guys,

I need a logic for below problem,

I was extracted some piece of text like "Name of the customer "
Example 1: sharukh khan and salman khan
Example 2: mumaith khan

i want a solution like
for ex1:
Firstname as : sharukh and salman
last name as : khan
for ex2:
firstname as : mumaith
lastname as : khan

please anyone can help me to solve this logic ,

Thanks in Advance

1 Like

I have a question … the last name always will be just one word?

@Sriram07, fisrt of all , welcome to our community!

Based on your example try this :

populated the variable string “strFullName” by using “get text” or using your method to set this string. After that, create 2 strings FirstName and LastName and use the assign activity

FirstName=strFullName.Split({" "},stringSplitOptions.RemoveEmptyEntries)(0)
LastName=strFullName.Split({" "},stringSplitOptions.RemoveEmptyEntries)(1)

~Diego Turati

@carmen
yeah !

@DiegoTurati
thanks for the reply!!!
In some documents it will vary like
ex1: salman r khan and sharukh r khan
soln : fname = salman r and sharukh r
lname = khan
Ex2 : salman khan
fname : salman
lname : khan
Ex 3 : mumaith khan and salman khan
fname : mumaith and salman
lname : khan
Actually i need logic for how can i split that EX1 as fname as salman r and sharukh r
And effectively i want to find the common word in Ex1 and Ex2 how can i find the common words in one string thats my lastname easily!!!

1 Like

@Sriram07 then … You also could try with Substring doing this …

firstName = FullName.Substring(0,fullname.LastIndexOf(" “))
lastName = FullName.Substring(FullName.LastIndexOf(” ")+1)

Let us know if it works for you.

2 Likes

@carmen

Its helped me to solve thank you and i want to know that how to find the common words in a string like
Ex1 : amithabh bachan and abishek bachan
soln should be “bachan

will you help me to solve this?

@Sriram07 I think that is already solved … please refer to this link here

1 Like

Hi @Sriram07 ,

In order to get the common names, first split each word in the string and add to an array. Then compare 2 arrays.
for eg,
Assign String array, Str_Array1 = String1.Split(" "c)
String Array, Str_Array2=String2.Split(" "c)
If you want to get the common names as a single string,
Assign String Common= String.Join(" ",Str_Array1.Intersect(Str_Array2).ToArray)
Just like i/p → “sharukh s khan” and “salman s khan” ;output string= “s khan”

If you want to get each common word separately,
Assign string array, Common_Arr= Str_Array1.Intersect(Str_Array2).ToArray
Common_Arr(0), Common_Arr(1) etc will return the common values, otherwise loop through a “for each” activity in the array “Common_Arr”.

Warm regards,
Nimin

1 Like