Java Data Type How to - Capitalize a string


We would like to know how to capitalize a string.


//  w w  w .  ja v a 2s  .  c o  m
public class Main {

  public static void main(String[] args) {
    String s = "this is a test";


  public static String capitalize(String s) {
    if (s.length() == 0) {
      return s;

    return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();

The code above generates the following result.