aspose file tools
The moose likes Beginning Java and the fly likes Replacing the the last character of the given String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Replacing the the last character of the given String" Watch "Replacing the the last character of the given String" New topic
Author

Replacing the the last character of the given String

Sanny kumar
Ranch Hand

Joined: May 18, 2005
Posts: 53
Hi guys

i want to write a program to replace the last charcter of the given string

for ex:

String s="wanting to replace last comma to dot,anf get the output,";
i want to get the output like this:

wanting to replace last comma to dot,anf get the output.;



replacing the last comma to .

any suggestins?
thanks in advance
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
If you always want to replace the last character, you could do something like this


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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 Morris
Java Q&A (FAQ, Trivia)
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
If you need more control and flexibility then use:

import java.util.regex.Matcher;
import java.util.regex.Pattern;



java j2ee job interview questions with answers | Learn the core concepts and the key areas
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[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
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing the the last character of the given String
 
Similar Threads
How to replace comma (,) with a dot (.) using java
need help
BigDecimal() issue
StringBuffer replace
Printing a string