DEV Community

Saravanan Muniraj
Saravanan Muniraj

Posted on

Convert all the first letters in a statement to capital letters

To convert all the first letters in a statement to capital letters, you can achieve this by checking if the addresses stored in the database are all in lowercase and then display them with the first letter capitalized using Java.

private static String capitalizeFirst(String word) {
    return word.substring(0, 1).toUpperCase()
      + word.substring(1).toLowerCase();
}


public static String capitalizeAllFirstLetters(String address){

    Pattern pattern = Pattern.compile("^[A-Z,a-z]");
    String[] addressArray = address.split(" ");
    StringBuffer afterChangesAddress = new StringBuffer();

    for(int i=0;i<addressArray.length;i++){
         Matcher m = pattern.matcher(addressArray[i]);
          if(m.find()){
              afterChangesAddress.append(capitalizeFirst(addressArray[i])).append(" ");
         }else{
             afterChangesAddress.append(addressArray[i]).append(" ");
         }
    }
    return afterChangesAddress.substring(0, afterChangesAddress.lastIndexOf(" "));

 }




public static void main(String[] args) {


    String str = capitalizeAllFirstLetters("1180 seven seats dr, orlando, fl 3283");

    System.out.println(str);

}
Enter fullscreen mode Exit fullscreen mode

Output:
1180 Seven Seats Dr, Orlando, Fl 3283

Top comments (0)