-1

I need some help with something, if someone is willing to help a newbie.

I have this piece of String[] here

String [] localitate = new String[]{"abrud", "aiud", "alba iulia", "albac", "almasu mare", "arieseni", "avram iancu"};

and this piece of code here to capitalize the first letter (I'm creating an AutoCompleteTextView)

for(int i = 0 ; i < localitate.length; i++)
{
    //localitate[i] = localitate[i].charAt(0).toUpperCase();
    localitate[i] = localitate[i].substring(0, 1).toUpperCase() + localitate[i].substring(1).toLowerCase();
} 

AutoCompleteTextView addressCity = (AutoCompleteTextView) findViewById(R.id.editText_objective_address_city);

ArrayAdapter<String> localitati = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, localitate);

Now, the thing is that, this code capitalizes only the first letter of the first word, but on the values with multiple words, does the same.

Can someone help me with this code so it can capitalize the first letter to all the words?

david
  • 3,225
  • 9
  • 30
  • 43

1 Answers1

1

What you need is to split each word to list of words, and then capitalize them, and then join them. Here it is:

for(int i = 0 ; i < localitate.length; i++)
{
    String tmp = "";
    String[] words = localitate[i].split(" ");
    for (int j = 0; j < words.length; j++) {
        tmp += words[j].substring(0, 1).toUpperCase() + words[j].substring(1).toLowerCase() + " ";
    }
    localitate[i] = tmp.trim(); // trim is to delete last space
} 

localitate will look like this:

[Abrud, Aiud, Alba Iulia, Albac, Almasu Mare, Arieseni, Avram Iancu]
david
  • 3,225
  • 9
  • 30
  • 43
Adnan Isajbegovic
  • 2,227
  • 17
  • 27