Help how to take the last word of a string and add it to the top using java

Asked

Viewed 314 times

1

I have a full name: Jais Pereira Guedes Godofredo

The example below, I get the last word, but how to print with this result: Godofredo, Jais Pereira Guedes

public class Aula1{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String nomeCompleto = "Jais Pereira Guedes Godofredo";
        String[] split = nomeCompleto.split(" ");
        String resultado = split[split.length - 1];
        System.out.println(resultado );
    }
}
  • 1

    I think it’s duplicate.

  • It can be related in part, it is wanting another relacionada which puts substring at the beginning.

3 answers

4

    String sentenca = "Jais Pereira Guedes Godofredo";
    int index= sentenca.lastIndexOf(" ");
    String restoSentenca = (sentenca.substring(0, index));
    String ultimaPalavra = (sentenca.substring(index+1));
    String novaSentenca = ultimaPalavra + ", " + restoSentenca;
    System.out.println(novaSentenca);

example - ideone

3


Hi @Amandarj. I’ve had this doubt, see if it helps you this, I solved it this way:

 *
 * @author marcia
 */
public class Aula1{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String nomeCompleto = "Jais Pereira Guedes Godofredo"; // Recebo o nome a ser tratado  
        int posicao = 0; // Posição do nome [0,1,2,3...], o nome está na posição 0  
        for (int i = 0; i < nomeCompleto.length(); i++) {
            if (nomeCompleto.charAt(i) == ' ') {
                posicao = i;
            }
        }
        System.out.println(nomeCompleto.substring(posicao, nomeCompleto.length()) + ", " + nomeCompleto.substring(0, posicao));
    }
}

-1

Use lastIndexOf and then substring

Follow some examples:

https://www.tutorialspoint.com/java/java_string_lastindexof.htm

https://www.tutorialspoint.com/java/java_string_substring.htm

Examples:

import java.io.*;
public class Test {

public static void main(String args[]) {

  String Str = new String("Welcome to Tutorialspoint.com");

  System.out.print("Return Value :" );

  System.out.println(Str.substring(10) );


}
}

import java.io.*;
public class Test {

public static void main(String args[]) {

  String Str = new String("Welcome to Tutorialspoint.com");

  System.out.print("Found Last Index :" );

  System.out.println(Str.lastIndexOf( 'o' ));

}
}

Source: www.tutorialspoint.com/java

  • 1

    This link may be a good suggestion, but your reply will not be valid if one day the link crashes. In addition, it is important for the community to have content right here on the site. It would be better to include more details in your response. A summary of the content of the link would be helpful enough! Learn more about it in this item of our Community FAQ: We want answers that contain only links?

Browser other questions tagged

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