javastring-utils

How to get UpperCamelCase using StringUtils?


I can't seem to get StringUtils.capitalize to actually capitalize my whole word string.

I've tried various ways, but I just end up getting sentence-type case. I tried using StringUtils.capitalize inside of what I want to print, but that doesn't work either. Nothing I look up helps me either.

  File file = 
      new File("C:\\Users\\mikek\\Desktop\\name.txt"); 
    BufferedReader abc = new BufferedReader(new FileReader(file));
List<String> data = new ArrayList<String>();
String s;
String t;
while((s=abc.readLine()!=null) {
    data.add(s);

    System.out.println("public static final Block " + s.toUpperCase() + "     = new "
+  StringUtils.capitalize(s).replace("_","") + "(\"" + s + "\", Material.ROCK);");
}

abc.close();
 }

Expected: Charcoal Block Got: Charcoal block


Solution

  • How about this??

            String s = "camel case word";
            String camelCaseSentence = "";
            String[] words = s.split(" ");
            for(String w:words){
                camelCaseSentence += w.substring(0,1).toUpperCase() + w.substring(1) + " ";
    
            }
            camelCaseSentence = camelCaseSentence.substring(0, camelCaseSentence.length()-1);
            System.out.println(camelCaseSentence);