-1

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;
   }
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 2
    Share code as text not as image please – azro Oct 26 '22 at 20:20
  • images are hard to debug, please share your code in a [mre] that illustrates the issue you are having – blurfus Oct 26 '22 at 20:21
  • Would you agree to regular expression to do a replacement ? That would be very easy – azro Oct 26 '22 at 20:21
  • @azro regular expression makes the job of the code easy, but it is not without a penalty. The penalty is that, in general, regex solutions are harder to maintain. Maintainability is a more costly attribute in relation to initial development cost. So, most likely, in the real world a regex solution will be impractical. – hfontanez Oct 26 '22 at 20:29

3 Answers3

2

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
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • This is the best solution! The trick here is use the capabilities of `StringBuilder` to do String manipulation. It is much easier than using the native `String` methods. I was going to post a similar answer. No need for that now. One minor suggestion, convert the character at index 0 to lower case in case the character is upper case. In the code I was going to post, I had that test case in it. – hfontanez Oct 26 '22 at 20:42
0

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();
}
001
  • 13,291
  • 5
  • 35
  • 66
0

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;
}
mmartinez04
  • 323
  • 1
  • 1
  • 4