Beliebte Suchanfragen
|
//

StringUtils.camelize(String);

25.2.2009 | 1 minutes of reading time

Today I noticed that the Apache Commons libs are lacking a method to camelize Strings.

Because I needed to convert such an XML name MY_TINY_PROPERTY into a Java property name myTinyProperty, I ended up writing a simple camelizer myself.

1private String toCamelCase(String value, boolean startWithLowerCase) {
2    String[] strings = StringUtils.split(value.toLowerCase(), "_");
3    for (int i = startWithLowerCase ? 1 : 0; i < strings.length; i++){
4        strings[i] = StringUtils.capitalize(strings[i]);
5    }
6    return StringUtils.join(strings);
7}

I guess this helpers covers most of the requirements. At least it is covering all that I need. As an alternative one could use the WordUtils#capitalizeFully() and postprocess the result.

The way back is even more difficult, because the delimiters for split() are a bit harder to determine. While many other programming languages feature a camelize and underscore method, Java (and Apache Commons) does not. Why? Should I propose a patch?

|

share post

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.