• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Replacing the the last character of the given String

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you always want to replace the last character, you could do something like this
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need more control and flexibility then use:

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

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Grow a forest with seedballs and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic