HOw to extract last word in an array

Hi, i am looking to extract the last word in an array. Sometime it will be the second word and other time the third word.
Clients who have middle names i will need the third word and for clients who have no middle name i will need the second word.

I need to extract the surname from the cell. pleasee see below

i have been able to the first part of the process in matching the first name initial from the cell to the webpage but i have no idea how i can do this with the surname. With the surname i will need to match the surname exactly unlike the first name where we are only matching the initial.

Below is a screen shot of my current workflow for this particular check

From given image I would suggest to split the string with WR. So the fist part would be the name.
Then split based on spaces and read the last part of it to get the surname.

1 Like

@Saahil_Chauhan that works in getting the first name but with the middle name being inconsistent there will be times it will pick the middle name to match with the webpage therefore thats not a solution i can used.

Is there a way to just get the surname and not have to think about the middle name like last.array for example?

Hi @nick.v,

Can you try the code below and see if it solves the requirement? :slight_smile:

Trim(valueFromCell.Substring(0,valueFromCell.IndexOf("WR"))).Split(" "c).Last


@jcastro am i using this in an assign activity? am not sure which activity i should be using this code in

Yup. Sorry I forgot to mention. You can use it in an Assign activity. That code returns the surname. :slight_smile:

aaahh ok thanks
so it would be MyArr = your code

before the surname if condition right? @jcastro

I’m not sure… What does MyArr contain? Is it the variable you’re using to store the surname retrieved from the cell?

Yes thats correct @jcastro

1 Like

am using google sheets and not excel would the code still work? @jcastro

1 Like

Hi @nick.v,

Yup it should work. As long as you’re able to get the data from google sheets, store them in a datatable, then loop through them. :slight_smile:

i am getting an error saying the "valueFromCell’ isnt declared should i be getting that?


hi @nick.v,

Where do you store the text retrieved from each cell? Are you using a For Each to go through the datatable? If so you can change the “valueFromCell” variable to the one where you stored the cell’s text

1 Like

Topman code worked perfectly @jcastro thank you

1 Like

i have received this error

how do i rectify this?

If it can’t match the name is should just record that and move on but the bot is stopping with an error

Hi @nick.v,

What is the string currently being processed when it errored out? I’m thinking it probably does not have the WR which is why it’s erroring out. Can you try checking the value of the Name.IndexOf("WR").ToString via Log Message activity then rerun using the string that errors out?

i checked this by putting the variable into a message box an you were right it doesn’t have WR in it. @jcastro

Ok. So how does the string being processed looks like? I’m using the WR as the basis - whatever comes before the WR is the surname.

yeah the first name followed by any middle name and then the surname. i only want the surname @jcastro

Ok so what we can do is we introduce an IF statement with below condition:

Name.IndexOf("WR") <= 0

In the Then part, use below code in an assign activity:

MyArr = Trim(Name.Split(" "c).Last)

In the Else part, use the Assign activity that’s getting an error. :slight_smile: