Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign values to two variables after split

Tags:

java

split

I have full name (ex : Robert King) separated by space in Database. I want to split the full name to FIRSTNAME and LASTNAME and assign it to two variables. How to achieve this in JAVA? I know it is simple but found it difficult to assign to two variable after split.

like image 871
Nash Avatar asked Nov 30 '22 05:11

Nash


1 Answers

You can only assign to a single variable in java, so you'll need two steps:

String fullname = "Robert King";
String[] names = fullname.split(" ", 1); // "1" means stop splitting after one space
String firstName = names[0];
String lastName = names[1];
like image 69
Bohemian Avatar answered Dec 06 '22 10:12

Bohemian