1 /** 2 * Copyright (C) 2006 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.inject.util; 18 19 /** 20 * String utilities. 21 * 22 * @author crazybob@google.com (Bob Lee) 23 */ 24 public class Strings { 25 26 /** 27 * Returns a string that is equivalent to the specified string with its 28 * first character converted to uppercase as by {@link String#toUpperCase}. 29 * The returned string will have the same value as the specified string if 30 * its first character is non-alphabetic, if its first character is already 31 * uppercase, or if the specified string is of length 0. 32 * 33 * <p>For example: 34 * <pre> 35 * capitalize("foo bar").equals("Foo bar"); 36 * capitalize("2b or not 2b").equals("2b or not 2b") 37 * capitalize("Foo bar").equals("Foo bar"); 38 * capitalize("").equals(""); 39 * </pre> 40 * 41 * @param s the string whose first character is to be uppercased 42 * @return a string equivalent to <tt>s</tt> with its first character 43 * converted to uppercase 44 * @throws NullPointerException if <tt>s</tt> is null 45 */ 46 public static String capitalize(String s) { 47 if (s.length() == 0) 48 return s; 49 char first = s.charAt(0); 50 char capitalized = Character.toUpperCase(first); 51 return (first == capitalized) 52 ? s 53 : capitalized + s.substring(1); 54 } 55 } 56