com.mycila.jmx.StringUtils Maven / Gradle / Ivy
/**
* Copyright (C) 2010 Mycila ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycila.jmx;
/**
* @author Mathieu Carbou ([email protected])
*/
final class StringUtils {
private StringUtils() {
}
public static boolean hasLength(String s) {
return s != null && s.length() > 0;
}
/**
* Capitalize a String
, changing the first letter to
* upper case as per {@link Character#toUpperCase(char)}.
* No other letters are changed.
*
* @param str the String to capitalize, may be null
* @return the capitalized String, null
if null
*/
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}
/**
* Uncapitalize a String
, changing the first letter to
* lower case as per {@link Character#toLowerCase(char)}.
* No other letters are changed.
*
* @param str the String to uncapitalize, may be null
* @return the uncapitalized String, null
if null
*/
public static String uncapitalize(String str) {
return changeFirstCharacterCase(str, false);
}
public static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0)
return str;
StringBuilder sb = new StringBuilder(str.length());
if (capitalize) {
sb.append(Character.toUpperCase(str.charAt(0)));
} else {
sb.append(Character.toLowerCase(str.charAt(0)));
}
sb.append(str.substring(1));
return sb.toString();
}
}