Kristian Kraljić You may compare programming with poetry,
a harmonic script is a piece of creativity which will last forever.
- Kristian Kraljić

Java Characters class

by Kristian Kraljic, December 31, 2015

This is going to be my last post in 2015, as New Year’s Eve quickly approaches. I also have to hurry up a little, as the raclette grill is already heating up. *jummy* ;-)

Time for a little year-end review. This year has again been very successful for me. After finishing my Master’s Degree in Computer Science end of last year, I began working as a research associate at the University of Applied Sciences in Mannheim next to my main job. We made great progress and were able to release the Genetic Code Anylsis Toolkit just recently. Some of the code snippets of the past few days, have been part of the work done for the Genetic Code Analysis Toolkit. Other than that I mainly focussed on my primary workplace, where I was working hard towards a first release of the SAP ONE Support Launchpad. A lot more happened, but as our little marathon soon comes to an end, let’s get to the point of this post.

There are a lot of helper classes in Java. Even in the core libraries you’ll find plenty of them. Classes such as Arrays, or the Collections class dealing with lists, sets and maps. One helper class I was missing out on, is a Characters class!

I often face the problem to have a String left or right trimmed for a certain character or a certain class of characters (e.g. whitespaces or new-line characters). Also other character based operations, such as finding the first occurrence of a character, or condensing multiple subsequent occurrences of a character into one, are commonly required. All of this can be achieved using the existing String methods and regular expressions, but for each character it’s again the same patterns to use and the same methods to call. Therefore I thought, why not creating a Characters helper class in Java, dealing with all of this?

Said and done. Please find my neat little implementation of a Characters helper class on GitHub at https://github.com/kristian/characters. With this dealing with characters becomes a walk in the park:

Characters.valueOf('X').trim('XXXHello WorldXXX');
Characters.WHITESPACE.condense('Foo  Bar  Baz');

I hope you had a great year 2015, best wishes for the New Year! See you in 2016.

Comments