I've said before that every Java project ends up writing (or using) a StringUtil class: a bunch of static methods to do things to Strings that Sun's class doesn't cater for.
Today, I found myself wanting to do a pretty basic String operation that wasn't on the main class, so I sent IDEA off hunting... and there were seven classes in my Classpath called either StringUtil or StringUtils, all of them from different projects.
So I wrote the method myself. Finding the one I should have been using amongst that lot was just too much effort :)
Sometimes I think there should be another level of class visibility: "only visible to classes loaded from the same location (jar, file classpath base, URL codebase)". That would make it easier to navigate a library: all the glue classes, helpers, utils and impls would be safely stowed away, and you'd just be browsing the real library interface.
Then, I think how much this could be abused by overzealous information hiders to make it impossible to do anything with a library that wasn't explicitly forseen by its author, and I realise it's a bad idea after all.
You'll also probably notice that most of these StringUtil classes will contain the same kind of methods. It always struck me that Java's rich class library didn't include more in the way of string utils - I remember the features provided my C for string manipulation were better than the object methods provided by the String class. You finding so many different implementations from different packages (I found 3 StringUtils classes on ym build path incidentally) is a clear sign that this really should be something addressed in the JDK itself.
This is funny, because several of our projects have StringUtils classes (and ByteUtils too, for that matter). Definitely a weak point in the Java API.
When doodling at home, I find myself writing/wanting those Util classes because I want my code more Smalltalk-like; I can't add behavior to the core classes so I'm left with the only option of creating Util classes.
What is your favourite StringUtil class and what does it provide?
(And ofcourse where can i download it :)
What is your favourite StringUtil class and what does it provide?
(And ofcourse where can i download it :)
The correct course of action is of course lobby to have the methods added to the JDK. Until that happens, write your own. That's what you aer paid to do anyway.
The wrong way of handling this is to hunt the Internet for someone elses StringUtil and use that. It's not worth the extra dependencies introduced.
Just my two cents.
--
Weiqi
I disagree Weiqi. You aren't paid to code necessarily, you are paid to solve problems. And if I can do that with a pre-written/pre-tested library then that I how I am usually going to solve the problem. Espacially if there isn't any bad licensing or dependancy problems. Jakarta Commons Lang for example http://jakarta.apache.org/commons/lang.html has a lot of good String functions and has no dependancies that I am aware of. Taking a page from the Pragmatic Programmers, i think a small but frequent amount of your time should be spent reviewing the capabilities of Util like libraries. Kind of a way to mentally take stock of what is possible. File it away in your brain so that you can recall it when necessary.
Yes, I was also thinking commons-lang. I even have this bad habit of replacing "stringutils" from the projects I'm working on, with commons-lang.
"The correct course of action is of course lobby to have the methods added to the JDK"
Possibly not. A large amount of Commons Lang StringUtils is formed from the Jakarta String Taglib. In turn, this has, I believe, pushed for a selection of string-based functions in the JSTL's JSP-function list. In turn I hope this will encourage the java.lang.String code to grow.