Kamlesh Sharma
Kamlesh Sharma

Reputation: 222

How to get the splitted string from split() method

Hi friends I m trying to obtain the splitted first name and last name obtained from the full name entered by user in my jsp form. After receiving the full name on submitting the form , I am splitting the fullname into two strings which is splitted with whitespaces delimiters. Here's the code to explain my problem .Please help

           String name = request.getParameter("name");
            String userType = request.getParameter("usertype");
            //split the name into first_name and last_name
            String  splittedName[] = StringUtils.split(name);

            System.out.println(Arrays.toString(splittedName));

            String firstname = splittedName[0];
            String surname = splittedName[1];

on debugging the application with breakpoints at the all the lines . I get error when i try to get splittedName[0] and splittedName[1] values into 'firstname' and 'surname' string. Please help ...sorry for any silly mistakes in my question.

Upvotes: 1

Views: 757

Answers (1)

Sharique
Sharique

Reputation: 859

   String name = "Kamlesh Sharma";

    String splittedName[] = name.split(" ");
    System.out.println(Arrays.toString(splittedName));

    String firstName = splittedName[0];
    String lastName = splittedName[1];

This will serve your purpose.

Note: You need to provide delimiter in split method and not a name which you're passing

Upvotes: 0

Related Questions