Obtenga el primer carácter de String Java
var string = "freeCodecamp";
string.charAt(0); // Returns "f"
Red Necked Phalarope
var string = "freeCodecamp";
string.charAt(0); // Returns "f"
// Array of names
String name [] = {"Clark", "Bruce", "Diana", "Barry"};
// for each loop (to iterate everything inside the array)
for (String names: name) {
System.out.println(names.charAt(0));
}
/* Prints
C
B
D
B
*/
public String firstElement(String[] array)
{
if (array.length >= 1)
{
return array[0];
}
else {
return "";
}
}