How to get a substring from a full string?

i have the following variables:
Name_1=“Jane Tan Jia Hui”
Name_2=“Lim Hui Hui”

How do i spilt the variables into 2 major substring of “Jane” as First Name then “Tan Jia Hui” as given name and “Lim” as First Name then “Hui Hui” as Given Name for the scenario? (always take the first element as First Name and the remaining as Given Name)

Hi,

There are some ways to achieve it. Hope the following expressions help you.

First name

System.Text.RegularExpressions.Regex.Split(text,"(?<=^\S+)\s")(0)

Given Name

System.Text.RegularExpressions.Regex.Split(text,"(?<=^\S+)\s")(1)

Another solution

First name

text.Substring(0,text.IndexOf(" "))

Last name

text.Substring(text.IndexOf(" ")+1)

Regards,

4 Likes

Definitely agree with Yoichi! The second solution is a bit easier to work with if you are just starting out.

Thank you for your help. The 2nd solution works perfectly fine.

1 Like

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