简体   繁体   English

在java中的String中的每个单词中首字母大写(但忽略特定单词)

[英]Capitalizing first char in every word in a String in java (but ignoring specific word)

Can anyone please tell me why the following code prints 1 the high street and not 1 The High Street?: 任何人都可以告诉我为什么下面的代码打印1高街而不是1高街?

    String propertyPageTitle = "1-the-high-street";
    propertyPageTitle = propertyPageTitle.replace("-", " ");
    WordUtils.capitalizeFully(propertyPageTitle);
    System.out.println(propertyPageTitle);

EDIT to show solution: 编辑以显示解决方案:

    String propertyPageTitle = "1-the-high-street";
    propertyPageTitle = propertyPageTitle.replace("-", " ");
    propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
    System.out.println(propertyPageTitle);

Supposing I wanted to ignore the word 'and' if it appears (I'm reading values from a .csv) and NOT change to titlecase? 假设我想忽略单词'和',如果它出现(我正在读取.csv中的值)而不是更改为标题? how would that be possible. 怎么会这样呢?

WordUtils.capitalizeFully不会更改原始String,而是返回大写字符串。

propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);

This happens because capitalizeFully(String) of WordUtils returns a String which has the expected answer. 发生这种情况是因为WordUtils capitalizeFully(String)返回一个具有预期答案的String So try: 所以尝试:

propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);

And then it will work. 然后它会工作。

String firstStr = "i am fine";
String capitalizedStr = WordUtils.capitalizeFully(firstStr);
System.out.println(capitalizedStr);

The return should be taken to get the output of a method. 应该返回以获取方法的输出。 It is common for all methods in Java String Java String中的所有方法都很常见

    String toBeCapped = "1 the high street and 2 low street";

    String[] tokens = toBeCapped.split("\\s");
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < tokens.length; i++) {
        if (!tokens[i].equalsIgnoreCase("and")) {
            char capLetter = Character.toUpperCase(tokens[i].charAt(0));
            builder.append(" ");
            builder.append(capLetter);
            builder.append(tokens[i].substring(1, tokens[i].length()));
        } else {
            builder.append(" and");
        }
    }
    toBeCapped = builder.toString().trim();
    System.out.println(toBeCapped);

output: 输出:

1 The High Street and 2 Low Street

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java将字符串数组中每个单词的第一个字母大写 - Java capitalizing first letter of every word in string array 将字符串中的每个其他词大写 - Capitalizing every other word in a string 大写字符串的第一个字符 - Java - Capitalizing the first character of a string - Java 如何将java中字符串中每个单词的第一个和最后一个字母大写 - How to capitalize the first and last letters of every word in a string in java 在 java 中的特定字符后替换特定单词 - replace specific word after specific char in java (Java)将以特定单词开头和结尾的每个字符串存储到数组中 - (Java) storing every string that starts and ends with a specific word into an array Caesar Cipher 在给定字符串中的第一个空格之后开始在该空格之后的每个单词的开头添加一个字符 ( &#39;C&#39; ) - Caesar Cipher after the first space in a given String starts adding a char ( 'C' ) to the beginning of every word after that space 在java中计算特定的char,但我的程序只读取单词中的第一个字符 - Counting a specific char in java, but my program only reads the first character in a word 如何将字符串中的每个单词大写 (java) - How to capitalize every word in a string (java) 如何在字符串中每个单词的第一个字母大写,同时在Java中删除最终的空格? - How to upper case every first letter of word in a string, meanwhile removing eventual whitespaces in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM