Convert array elements to camel case in Java

Hello, coders today in this tutorial we learn how to convert array elements to camel case in Java. This also means how to convert first letter of each word to uppercase in an array in Java.

You can check this tutorial: Converting first letter of each word in a sentence to upper case in Java

What is CamelCase?

First of all, we will learn or understand what is meant by Camel case, Camelcase is also known as camel caps.

The term CamelCase is a name formed by joining two or more words with their first letter as capital of each word so that each word that makes up the name can easily be read.

Examples for CamelCase: FedEx, HarryPotter, NetFlix.

EXPLANATION:

First lets create an array {“hi there”,”hello buddy”}

next, we will create a function which will split or exclude the first word to capitalize it we are gonna use [ array.split(” “) ].

after that, we are gonna make the first-word capital using [ string.join(” “, strArray) ] , Now we are gonna make first letter capital to do that we will create a function makeFirstLetteCapitalForEachWord.

and finally, we will print the result by calling the functions.

Here is the output {“Hi There”, “Hello Buddy”}

Below is the code to convert an array of string to Camelcase in Java

import java.util.Arrays;
public class Main {
    public static void main(String[] args) {
        String[] arrays = {"hi there","hello buddy"};

        for (String array: arrays) {
            String result = makeCamelCase(array);
            System.out.println(result);
        }

    }
    static String makeCamelCase(String array ) {
        String[] strArray = array.split(" ");
        makeFirstLetterCapitalForEachWord(strArray);
        String result = String.join(" ", strArray);
        return result;
    }

    static void makeFirstLetterCapitalForEachWord(String[] inStrings) {
        for (int i = 0; i < inStrings.length; i++) {
            inStrings[i] = firstLetterCapital(inStrings[i]);
        }
    }

    static String firstLetterCapital(String str) {
        String firstLetter =  str.substring(0,1); 
      
        String firstLetterCapital = firstLetter.toUpperCase();

        String strExcludeFirstLetter = str.substring(1, str.length());

        String result = firstLetterCapital + strExcludeFirstLetter;

        return result;
    }

}

 

 

INPUT:

{"hi there","hello buddy"}

OUTPUT:

{"Hi There","Hello Buddy"}

Also, read:

Leave a Reply