0

我似乎无法让 StringUtils.capitalize 真正将我的整个单词字符串大写。

我尝试了各种方法,但我最终得到了句子类型的案例。我尝试在要打印的内容中使用 StringUtils.capitalize,但这也不起作用。我查找的任何内容都对我没有帮助。

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

预期:木炭块得到:木炭块

4

2 回答 2

1

这个怎么样??

        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);
于 2019-08-02T21:21:56.477 回答
0

Chris Katric 帮我弄清楚了什么:

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

现在我得到(对于 System.out.println 的全部部分):“public static final Block CHARCOAL_BLOCK = new CharcoalBlock("charcoal_block", Material.ROCK);" 就像我想要的那样。

于 2019-08-03T04:01:35.053 回答