If you use the CharacterSequence interface, you can perform your desired task without performing a copy of all elements (which is what String.substring does) - taking advantage of avoiding one of many abstraction leaks of java.lang.String.
The source code to InsertSequenceImpl provides an example of how this is achieved (the different is that elements are typed by parameter and not fixed at type char). The source code is available in the download, or on test coverage report
[Tony]: If you use the CharacterSequence interface, you can perform your desired task without performing a copy of all elements
Is "CharacterSequence" one of your custom classes, or did you mean CharSequence? If it's the latter, I'd note that most current implementations of subSequence() from Sun's JDK's (in String, StringBuilder, StringBuffer) do the same copy Tony talks about avoiding. Some of the CharBuffer implementations may not. And of course one can make their own custom implementation of CharSequence. However this feels very very much like premature optimization, especially considering this post is in JiG Beginner.
Sanny: I like the regex solution suggested by AK Pillai. Though unfortunately, regular expressions are another thing non-obvious for beginners. Sanny, you may want to study the Java tutorial for more info on regular expressions. (Or learn more from one of the several books out there, Friedl, Habibi...) Note that the String class has methods defined directly, which can streamline the code a bit from what AK showed:
Learning how regular expressions work takes some time, but I think it's time well spent since it gives you many flexible and pwoerful options fora problem like this. [ March 13, 2006: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Sep 24, 2003
Yes I did mean CharSequence. It is certainly not premature optimisation - it is restricting abstraction to all that is necessary (i.e. not exceeding requirements). Any performance gain is pure consequential and certainly not a primary (or secondary or anything) objective at all. It is not coincidental however, that aligning with requirements quite often improves algorithmic performance. An expression of formal proof of requirements can obviate some clear performance gains.
In any case, implementing such a thing for CharSequence is entirely in vain because the contract states that one must implement the toString method, which means that the data copy must occur since String contains implicit abstraction leaks (imagine if toString returned CharSequence instead - the issue evaporates - this is one reason why all public operations should be declared on an interface). The best case scenario is creating the String (for toString to return) on demand - hoping that clients never invoke the method.
AntiObject sidesteps that whole convoluted issue quite bluntly and effectively.
For both simplicity and readibilty (and because the OP simply wants to replace the last character, not the last comma -- even though you could do that with the RegExp just as easily), I'd just use a StringBuffer
[ March 13, 2006: Message edited by: Joel McNary ]
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.