Delete last word on string after space

Hello, i have an automation problem

so I have a several product name which I want the robot to remove the last word in the product name (after space)
for example:
Reinforced Steel Bar
i want to delete the last word (“bar”) so it will become “Reinforced Steel”
Mirror (new)
i want to delete the last word (“(new)”) so it will become “Mirror”

So how can i achieve this?
Thank you in advance

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Replace(yourString,"\s\S+$","")

Regards,

1 Like

Hi @Fauzan_Marantama ,

Besides @Yoichi’s regex solution, another possible way to solve the issue would be:

stringVariable.Replace(" "&stringVariable.Split(" "c).Last,"")

Best regards,
Marius

2 Likes

This should also work:

Left(yourString.Trim,yourString.Trim.LastIndexOf(" "))

3 Likes

newStr=Str.Replace(Split(Str," “).Last.tostring,”").Trim

1 Like

This is the correct solution. So many people try to overcomplicate things with Regex and complicated expressions. Simple works.

which is the correct solution?

Like @jakub.brzezinski wrote, the correct solution that @postwick refers to is:

Left(yourString.Trim,yourString.Trim.LastIndexOf(" "))

what is “yourString”?

YourString is literally the Variable that you created to store the string.

Example:
If you created a Variable called Badget and you Assigned your BadgetName to it
Badget = Robot Apprentice
You create a variable with type string to store the result, lets say the cariable that you create is called OutBadget

when you use the formula

Assign Activity
OutBadget = Left(Badget.Trim,yourString.Trim.LastIndexOf(" "))

Know the value inside OutBadget will be only “Robot”