Help on how to catch the last word of a string and add to the beginning using java



  • I have a full name: Jais Pereira Godofre

    The example below, I get the last word, but how do 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 );
    }
    

    }



  •     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);
    

    https://ideone.com/3MqVsr




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2