Home > database >  I need help converting strings into camelCase with multiple "_" using loop
I need help converting strings into camelCase with multiple "_" using loop

Time:10-27

My code doesnt convert ex. dog_cat_dog into dogCatDog. The out put of my code is dogCat_dog. Trying to make a loop that doesn't stop at the first "_":

public String underscoreToCamel(String textToConvert) {
   int index_= textToConvert.indexOf("_",0);
   String camelCase="";
   String upperCase = "";
   String lowerCase="";
   for (int i=0; i < textToConvert.length(); i  ){
     if(i==index_){
       upperCase= (textToConvert.charAt(index_ 1) upperCase).toUpperCase();
       upperCase= upperCase  textToConvert.substring(index_ 2);
       
     }
     else{
       lowerCase=textToConvert.substring(0,index_);
     }
     camelCase=lowerCase upperCase;
     

   }
     
        return camelCase;
   }

CodePudding user response:

I would do the following: make the method static, it does not use any class state. Then instantiate a StringBuilder with the passed in value, because that is mutable. Then iterate the StringBuilder. If the current character is underscore, delete the current character, then replace the now current character with its upper case equivalent. Like,

public static String underscoreToCamel(String s) {
    StringBuilder sb = new StringBuilder(s);
    for (int i = 0; i < sb.length(); i  ) {
        if (sb.charAt(i) == '_') {
            sb.deleteCharAt(i);
            char ch = Character.toUpperCase(sb.charAt(i));
            sb.setCharAt(i, ch);
        }
    }
    return sb.toString();
}

I tested like

public static void main(String[] args) {
    System.out.println(underscoreToCamel("dog_cat_dog"));
}

Which outputs (as requested)

dogCatDog

CodePudding user response:

You can split on '_' then rebuild.

public static String underscoreToCamel(String textToConvert) {
    String [] words = textToConvert.split("_");
    StringBuilder sb = new StringBuilder(words[0]);
    for (int i = 1; i < words.length; i  ) {
        sb.append(Character.toUpperCase(words[i].charAt(0)));
        sb.append(words[i].substring(1));
    }
    return sb.toString();
}

CodePudding user response:

I think an easy way to solve this is to first consider the base cases, then tackle the other cases

public static String underscoreToCamel(String textToConvert){

    //Initialize the return value
    String toReturn = "";

    if (textToConvert == null){
        //Base Case 1: null value, so just return an empty string
        return "";
    } else if (textToConvert.indexOf("_") == -1) {
        //Base Case 2: string without underscore, so just return that string
        return textToConvert;
    } else {
        //Primary Case:

        //Find index of underscore
        int underscore = textToConvert.indexOf("_");

        //Append everything before the underscore to the return string
        toReturn  = textToConvert.substring(0, underscore);

        //Append the uppercase of the first letter after the underscore
        toReturn  = textToConvert.substring(underscore 1, underscore 2).toUpperCase();

        //Append the rest of the textToConvert, passing it recursively to this function
        toReturn  = underscoreToCamel(textToConvert.substring(underscore 2));
    }

    //Final return value
    return toReturn;
}
  • Related