Java Utililty Methods String Format

List of utility methods to do String Format

Description

The list of methods to do String Format are organized into topic(s).

Method

StringgetFormatted(String messageKey)
get Formatted
return RESOURCE_BUNDLE.getString(messageKey);
StringgetFormattedMessage(final String pattern, final Object[] arguments)
get Formatted Message
String formattedMessage = "";
final MessageFormat formatter = new MessageFormat(pattern);
formattedMessage = formatter.format(arguments);
return formattedMessage;
StringgetFormattedString(ResourceBundle b, String key, Object... params)
Encapsulates the logic to format a string based on the key in the resource bundle
return MessageFormat.format(b.getString(key), params);
StringgetFormattedString(String key, Object arg)
get Formatted String
return MessageFormat.format(getString(key), new Object[] { arg });
StringgetFormattedString(String p_bundleName, String p_key, Locale p_locale, Object[] p_arguments)
Retrieves a resource bundle string with substituted argument values using the default locale in the JVM
Locale locale = p_locale;
if (p_locale == null) {
    p_locale = Locale.getDefault();
String messagePattern = null;
try {
    ResourceBundle bundle = ResourceBundle.getBundle(p_bundleName, locale);
    messagePattern = bundle.getString(p_key);
...
StringgetFormatValue(String value, Object[] args)
get Format Value
if (value != null) {
    value = MessageFormat.format(value, args);
return value;
ImageWritergetImageWriter(ImageTypeSpecifier imageType, String imageFormatName)
Gets an image writer suitable to be used for the given image type and image format.
return getImageWriter(imageType, imageFormatName, null);
ImageWritergetImageWriter(String formatName)
get Image Writer
Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(formatName);
if (it.hasNext()) {
    return it.next();
return null;
intgetRequriedArgumentCount(MessageFormat msgFormat)
Returns the number of required arguments for the given MessageFormat.
Format[] formats = msgFormat.getFormatsByArgumentIndex();
int required = formats.length;
MessageFormat mf = new MessageFormat("");
for (Format fmt : formats) {
    if (fmt instanceof java.text.ChoiceFormat) {
        mf.applyPattern(((ChoiceFormat) fmt).toPattern());
        required = Math.max(required, getRequriedArgumentCount(mf));
return required;
StringgetStylingHyphenFormat(String cssProperties)
Converts CSS properties to hyphen format.
if (cssProperties == null) {
    return null;
StringBuilder returnStr = new StringBuilder();
String[] properties = cssProperties.split(";"); 
if (properties == null || properties.length == 0) {
    return cssProperties;
for (int j = 0; j < properties.length; j++) {
    if (j != 0) {
        returnStr.append(";"); 
    String[] pair = properties[j].split(":");
    if (pair == null || pair.length <= 1) {
        returnStr.append(properties[j]).append(";");
        continue;
    List<String> words = new ArrayList<String>(3);
    int begin = 0;
    int i = 0;
    for (; i < pair[0].length(); i++) {
        if (Character.isUpperCase(pair[0].charAt(i)) && i != 0) {
            words.add(pair[0].substring(begin, i).toLowerCase());
            begin = i;
    if (begin != i) {
        words.add(pair[0].substring(begin, i).toLowerCase());
    StringBuilder sb = new StringBuilder();
    i = 0;
    for (i = 0; i < words.size(); i++) {
        if (i != 0) {
            sb.append("-");
        sb.append(words.get(i));
    returnStr.append(sb);
    for (i = 1; i < pair.length; i++) {
        returnStr.append(":").append(pair[i]);
return returnStr.toString();