GenJavaCore's StringW.javaStringW is an extensive String library from generationjava.com. It exists because the Java language's String and StringBuffer combination are quite limited. The W in StringW stands for 'Wrapper', indicating that it is a wrapper class for a standard Java class. There are many other 'W' classes available from generationjava.com. StringW was created by looking at the Ruby, C# and PHP languages, and adding any string methods contained within. It was created initially due to a request on a newsgroup for PHP-string functions in JSP. A slight variant on StringW is available from Apache Jakarta's Commons project. In particular it is in the Lang sub-project in a component named org.apache.commons.lang.Strings. The major differences are that a couple of the methods have been refactored out of the class [and into the Codec sub- project and a few methods have been renamed. ] Examplesjava.lang.String methodsAll the methods in the StringW class are static and stateleess, indeed I like to refer to this type of method as a function rather than a method as it is rather an old-school non-OO thing to do. The simplest functions are ones that map directly to java.lang.String. These exist in StringW so as to allow StringW to provide additional functionality such as null-checking, or bug-fixing. These methods are:
CapitalisationThe first set of real functions are the capitalisation functions. These are usually the first functions that anyone puts in their StringUtil class and there's no difference here. Two things of note are that firstly I use the English spelling and not the American spelling and that secondly I implement capitalise 'correctly', that is I use toTitleCase and not toUpperCase. This makes StringW.capitalise internationalised. I hope.
ReplacementThe next function that people write for their StringUtil class is a replace function, and yet again there are no great surprises here. There are three replace functions of note.
*plodingThe third and final common function to find in a StringUtil is the explode
or split function. The job of this function is to take a String with a
common delimiter, such as "one,two,three", and split it up into an array of
Strings. The naming here can lead to religious debates. A lot of people
(especially those with perl backgrounds) like to use split/join as their
function names. These suffer from a problem in that 'join' is already a
method on java.lang.Object, and although the arguments are different it
seems poor form to overload it. Split and unsplit are an option, although
semantically odd. I chose explode/implode due to having used those in
previous languages. For the Apache project I agreed to rename them to
split/join so the versions in StringW may change in the future.
Chomping
A commonly used function in perl is the chomp function. Its most common use
is to remove newlines from lines of text, but it's in fact far more
powerful. The best place for its use is to handle easy indexOf/substring
things. For example:
StringW.getPrechomp("foo:bar",":")+StringW.prechomp("foo:bar",":")
is in fact "foo:bar:, ie) the string doesn't change.
If not given a paramter, chomp will remove the last newline and everything
after it, in the same fashion as perl's chomp.
The chop method is slightly different. It removes the last character, although
if it finds a \r\n it will remove both of these. The chopNewline function
does the same thing, except it will only remove the \r\n or \n values, so
anything else stays safe.
Lastly there is chompLast. This is pretty similar to chop except that it can
handle more than one character. It is a chomp function that only works if the
passed in delimiter (or automatic newline) is at the end of the line.
This may seem as though there are lots of redundant functions, in fact that
is true. It is an example of the 'There's More Than One Way To Do It'
philosophy that Perl holds to heart.
TrimmingThis section does not include the eponymous trim function, but does include lots of trim-like functions. They are:
Padding/AligningA lot of people create pad functions in their StringUtils, and yet again, StringW is no different. There are two functions available to center a String inside a larger string, there are two leftPad functions and two rightPad functions. Lastly there is a repeat function and a function for overlaying a String on top of another String.
StringW.overlayString("onelongpieceoftext", "short", 3, 7)
will result in "oneshortpieceoftext". Effectively the overlayString function
is the indexOf equivalent of a replace function. It's weaker, but allows more
precision.
Random text creationStringW has a powerful set of functions to output random bits of text. It can output a random (length specified) string of numbers, ascii, letters or alphanumerics. It can also take a set of characters to randomise, and randomise over a range of unicode character, making such things as random chinese passwords possible.
CharSet manipulationsThe StringW class provides a set of features linked to a CharSet class. This class allows a set of characters to be described as: { "a-z", "d", "dqw" }. The available functions handle deleting, counting, translating and squeezing characters.
Soundex'sOne feature that is not quite String-based, but kind of, is the soundex algorithm which produces a hash-code type String based on the sound of the word. The metaphone algorithm is a more advanced form of the soundex algorithm. StringW provides both:
ReversesThree simple functions which reverse a String. The first is just a reverse, the second reverses a String based on a delimiter, so: com/genjava/test becomes test/genjava/com. The last is a dedicated method to reversing dotted names, like a Java classname, ip address or domain name.
BooleansStringW provides a bunch of boolean functions to interrogate a String. They're quite obvious I hope.
Index OfsThese guys come from .Net. Basically they find the first index of a set of possible values. So you can ask, what is index of the first vowel. These are a good candidate to become CharSet methods [see above].
EscapingA couple of ones to handle escaping either the Java escapes, ie) \t etc, or regular expression characters. XML/HTML escaping is handled by XmlW.
MiscellaneousHere are the, still very useful, methods which lack a good grouping.
Firstly we have defaultString. This allows a developer to handle nulls.
StringW.defaultString(vari) will return "" if vari is null.
|