-
-
Save parzibyte/0412d6a4b1242cb7cbfc23ff5e869434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String capitalizar(String cadena) { | |
// Separar palabras de la cadena | |
String[] separadaPorEspacios = cadena.split(" "); | |
StringBuilder sb = new StringBuilder(); | |
for (int indice = 0; indice < separadaPorEspacios.length; indice++) { | |
String palabra = separadaPorEspacios[indice]; | |
// De la palabra, primero agregar la primera letra ya convertida a mayúscula | |
char primeraLetra = palabra.charAt(0); | |
sb.append(Character.toUpperCase(primeraLetra)); | |
// Luego agregarle "lo sobrante" de la palabra | |
sb.append(palabra.substring(1)); | |
// Y si no es el último elemento del arreglo, le añadimos un espacio | |
if (indice < separadaPorEspacios.length - 1) | |
sb.append(" "); | |
} | |
// Finalmente regresamos la cadena | |
return sb.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment