Capitalize a String - Java java.lang

Java examples for java.lang:String Capitalize

Introduction

The following code shows how to capitalize the first letter for a String.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(capitalize(str));
    }//from   ww w .j  a  v a2  s . com

    public static String capitalize(String str) {

        if (str == null || str.isEmpty()) {
            return "";
        }

        char[] buffer = str.toCharArray();
        buffer[0] = Character.toTitleCase(buffer[0]);

        return new String(buffer);
    }
}

Related Tutorials