Converting any String to Camel Case


Hey guys, this is the code for Converting any String to Camel Case String. I found this code extremely useful.







 
/**
  * Accepts any String & converts in into Camel Case String
  * 
  * @param text
  * @return
  */
 public String convertToCamelCase(String text) {
  String result = "", result1 = "";
  for (int i = 0; i < text.length(); i++) {
   String next = text.substring(i, i + 1);
   if (i == 0) {
    result += next.toUpperCase();
   } else {
    result += next.toLowerCase();
   }
  }
  String[] splited = result.split("\\s+");
  String[] splited1 = new String[splited.length];

  for (int i = 0; i < splited.length; i++) {
   int l = splited[i].length();
   result1 = "";
   for (int j = 0; j < splited[i].length(); j++) {
    String next = splited[i].substring(j, j + 1);

    if (j == 0) {
     result1 += next.toUpperCase();
    } else {
     result1 += next.toLowerCase();
    }
   }
   splited1[i] = result1;
  }
  result = "";
  for (int i = 0; i < splited1.length; i++) {
   result += " " + splited1[i];
  }
  return result;
 }


Have fun... !!

"Knowledge without justice ought to be called cunning rather than wisdom" - Plato

No comments :

Post a Comment