在Java中将字符串转换为标题大小写

使用Apache commons 类,String.split()方法和Java 8字符数组 Stream 将字符串转换为标题的 Java示例。WordUtils

1.将字符串转换为标题大小写– WordUtils

Apache commons-text提供WordUtils来对包含单词的String执行操作。

1.1。Maven依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.4</version>
</dependency>

1.2。WordUtils示例将字符串转换为标题大小写

public class StringExample 
{
    public static void main(String[] args) 
    {

        final char[] delimiters = { ' ', '_' };

        WordUtils.capitalizeFully(null, delimiters);
        WordUtils.capitalizeFully(" ", delimiters);
        WordUtils.capitalizeFully("a", delimiters);
        WordUtils.capitalizeFully("thor almighty", delimiters);
        WordUtils.capitalizeFully("string operation", delimiters);
        WordUtils.capitalizeFully("TITLE CASE CONVERSION", delimiters);
    }
}

程序输出:

null
A
Thor Almighty
String Operation
Title Case Conversion

2.将字符串转换为首字母大写– String.split()和StringBuffer

要使用String.split()方法将字符串转换为Java中的标题大小写,请使用以下逻辑:

  • 将第一个字符转换为大写
  • 将字符串的其余部分转换为小写
  • 将结果附加到StringBuffer空格(“”)之后
  • 返回结果字符串
public class StringExample 
{
    public static void main(String[] args) 
    {
    	titleCaseConversion(null);
    	titleCaseConversion(" ");
    	titleCaseConversion("a");
    	titleCaseConversion("thor almighty");
    	titleCaseConversion("string operation");
    	titleCaseConversion("TITLE CASE CONVERSION");
    }

    private static String titleCaseConversion(String inputString) 
    {
        if (StringUtils.isBlank(inputString)) {
            return "";
        }

        if (StringUtils.length(inputString) == 1) {
            return inputString.toUpperCase();
        }

        StringBuffer resultPlaceHolder = new StringBuffer(inputString.length());

        Stream.of(inputString.split(" ")).forEach(stringPart -> 
        {
            if (stringPart.length() > 1)
                resultPlaceHolder.append(stringPart.substring(0, 1)
                					.toUpperCase())
                					.append(stringPart.substring(1)
                					.toLowerCase());
            else
                resultPlaceHolder.append(stringPart.toUpperCase());

            resultPlaceHolder.append(" ");
        });
        return StringUtils.trim(resultPlaceHolder.toString());
    }
}

程序输出:

null
A
Thor Almighty
String Operation
Title Case Conversion

3.将字符串转换为标题大小写–字符数组 Stream

要将字符串转换为带有char数组的标题大小写,请使用以下逻辑:

  • 将字符串转换为小写
  • 将上面的小写字符串转换为字符数组
  • 将数组中的第一个字符转换为大写
  • 将所得的字符串从字符数组追加new String(charArray)StringBuffer后跟空格(“”)。
  • 返回修剪后的结果
public class StringExample 
{
    public static void main(String[] args) {

        titleCaseConversion(null);
    	titleCaseConversion(" ");
    	titleCaseConversion("a");
    	titleCaseConversion("thor almighty");
    	titleCaseConversion("string operation");
    	titleCaseConversion("TITLE CASE CONVERSION");
    }

    private static String titleCaseConversion(String inputString) 
    {
        if (StringUtils.isBlank(inputString)) {
            return "";
        }

        if (StringUtils.length(inputString) == 1) {
            return inputString.toUpperCase();
        }

        StringBuffer resultPlaceHolder = new StringBuffer(inputString.length());

        Stream.of(inputString.split(" ")).forEach(stringPart -> {
            char[] charArray = stringPart.toLowerCase().toCharArray();
            charArray[0] = Character.toUpperCase(charArray[0]);
            resultPlaceHolder.append(new String(charArray)).append(" ");
        });

        return StringUtils.trim(resultPlaceHolder.toString());
    }
}

程序输出:

null
A
Thor Almighty
String Operation
Title Case Conversion

saigon has written 1445 articles

One thought on “在Java中将字符串转换为标题大小写

Leave a Reply