简体   繁体   中英

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

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);

What Chris Katric helped me figure out:

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;

while((s=abc.readLine())!=null) {
data.add(s);
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("public static final Block " + s.toUpperCase() + " = new "
+  camelCaseSentence.replace(" ", "") + "(\"" + s + "\", Material.ROCK);");
}

abc.close();
 }

Now I get(for the full part of the System.out.println): "public static final Block CHARCOAL_BLOCK = new CharcoalBlock("charcoal_block", Material.ROCK);" just like I wanted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM