String Manipulation - split()

Asked

Viewed 2,280 times

6

Is there any way to give a split() in a string and on the same line take the position I want?

For example:

String nome = "Dan Lucio Prada";

String sobrenome = nome.split(" "); //aqui quero pegar só o sobrenome [Prada]
  • Despite having answered the question I consider that this has no interest or advantage, especially in the case of the reply of Jéferson Bueno that requires to make two calls to nome.split(" "); and have the most difficult code to read.

  • I fully agree with @ramaral, this is for study purposes only. I showed the solution that way just because it gets more dynamic, because I didn’t know if you were ever going to have the same string or she could change.

4 answers

8


All answers are correct, but I find it interesting to add that, taking into account that the surname will always be the last word of string, you can do the following:

String nome = "Dan Lucio Prada";
EscreverSobrenome(nome); // A saída será "Prada"

String nome2 = "Joaquim Pedro Soares da Silva";
EscreverSobrenome(nome2); // A saída será "Silva"

public String EscreverSobrenome(String nome){
    sobrenome = nome.split(" ")[nome.split(" ").length - 1]; // Essa linha é a solução pro seu problema
    System.out.println(sobrenome);
}

This way no matter how many words have, the variable sobrenome will always get the last word.

5

Yeah, do it like this:

String nome = "Dan Lucio Prada";
String sobronome = nome.split(" ")[2];

Of course, this only works if the name has three words.

  • String[] names = name.split(" ") String sobronome = names[names.lenght()-1];

  • @Christianberegula Yes, I was going to put something like that but the AP wants one instruction.

2

Yeah, you get in a line:

System.out.println("Dan Lucio Prada".split(" ")[2]);

But it’s much better to use more than one line and do it right:

String nome = "Dan Lucio Prada";
String[] partes = nome.split(" ");
System.out.println(partes[partes.length - 1]);

You can do this on a line as well but it’s inefficient, so you better do it the right way.

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

2

For registration and (maybe) use for someone, in this case there is no need to do split of string. I don’t know if the obligation is to use split or have a last name, but without using split it is possible to do in a single line, something like this:

sentence.substring(sentence.lastIndexOf(" ") + 1)

sentence.lastIndexOf(" "), as it is simple to understand, it will return the index of the last space in the sentence. Then we add 1, position of the next letter which is the first letter of the last name. With this information substring(int beginIndex), will start from this first letter of the name and go to the end of the sentence/name, always returning the last word.

For example, this:

public static void main(final String[] args) {
    final String dan = "Dan Lucio Prada";
    System.out.println(lastWordFromSentence(dan));

    final String bruno = "Bruno César";
    System.out.println(lastWordFromSentence(bruno));

    final String name = "Bruno";
    System.out.println(lastWordFromSentence(name));
}

public static String lastWordFromSentence(final String sentence) {
    return sentence.substring(sentence.lastIndexOf(" ") + 1);
}

Generates this result:

Prada
César
Bruno

It is suggested in cases where the use of split :)

Browser other questions tagged

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