I read in a C++ book once that you should have your own String Utils class file at your disposal.
Of course there are several people who have created a String Utils class out there on ther internet. Apache StringUtils comes to mind. But why not have something that you call your own?
Having programmed in ColdFusion for a number of years, I find that the left and right functions have been very useful. Of course there is nothing of the sort in Java.
So, here is my attempt at writing such methods.
The premise is simple. You take a string and specify how many characters from the left or from the right you wish to get from the string.
public class StringUtils {
public String left(String str, int num) {
return str.substring(0, num);
}
public String right(String str, int num) {
return str.substring(str.length() - num);
}
}
Now of course there are two ways that you can have your StringUtils accessed. Either by calling the class and the methods, like above, or by having the methods be static. The choice is yours.
No comments:
Post a Comment