Capitalize a string by using Character.toTitleCase - Java java.lang

Java examples for java.lang:String Capitalize

Description

Capitalize a string by using Character.toTitleCase

Demo Code

public class Main {

  public static void main(String[] argv) {
    String str = "java2s.com";
    System.out.println(capitalize(str));
  }//from   w w  w .  j ava 2  s  .c o m

  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