How do I get the First name from a given name, I am able get the last name using the lastIndexof function, but not the first name

How do I get the First name from a given name, For example: John Smith, I want to get only John. But I am able get the last name using the lastIndexof function. Could someone help me on this please.