Java fluent interface for chaining string methods together -
i'm attempting write java class perform basic string manipulation functions.
i want class callable in manner like:
string tmp = stringutils.removespaces(str).removeslashes(); or
string tmp = stringutils.removeslashes(str).removespaces(); i'm struggling figure out how structure class. guess method or variable within class static, , methods return 'this'. how removeslashes method return string 'tmp' if it's returning this? forced use removeslashes.tostring() or removeslashes.getstring() or effect. seems bit convoluted...
i'd appreciate if me method definitions , return types.
this might started.
public class stringutil { public static void main(string args[]) { string url = "http://something.com/ \\ponies"; string newvalue = stringutil.str(url).removeslashes().removespaces().uppercase().getvalue(); system.out.println(newvalue); } private string value; public stringutil(string value) { this.value = value; } public static stringutil str(string value) { return new stringutil(value); } public stringutil removeslashes() { value = value.replace("\\", ""); return this; } public stringutil removespaces() { value = value.replace(" ", ""); return this; } public stringutil uppercase() { value = value.touppercase(); return this; } public string getvalue() { return value; } }
Comments
Post a Comment