N.Sara N.Sara - 2 months ago 7
Java Question

How to determine the first letter of these strings?

I am trying to write a for loop to print all the elements that start with A. How do I determine the first letter of these strings and compare it to A?

String[] things;
things = new String[] {"Apple", "Ant", "Aardvark", "Chocolate", "Football", "House", "Aluminium"};

Answer
for(int i =0 ; i < things.length;i++)
{
if(things[i].toLowerCase().startsWith("a"))
{
   // string starts with a
}
}