How to store the returned Strings of the split() method in different variables?

Asked

Viewed 169 times

3

I have the following problem: register a person informing the Name and CPF, in this format "João Medeiros - 123456789-00", and stored in a variable of type String. I used the method split() to "separate" Name and CPF.

How do I store the Name in the variable String nome, and CPF in variable String CPF?

1 answer

7


When using the method split your string becomes an array, then just create the variables and set which position of the array you assign value to that variable.

String frase = "João Medeiros - 123456789-00";
String array[] = frase.split(" - ");
String Name = array[0]; //Aqui vem o nome como primeira posição
String CPF = array[1]; //Aqui vem o CPF como segunda posição
  • Your solution worked out!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.