| Author |
Replacing string
|
sankar dunga
Greenhorn
Joined: Mar 07, 2011
Posts: 16
|
|
Dear sir,
How can we replace old string with new string characters from right side , i mean
.....................
If the oldstrring=00000000
and newstring=1100
i want the output as 00001100.
................................
please give me if any functions are available to do this.
Thank you
regards
sankar
|
 |
Mahi Ranga
Ranch Hand
Joined: Jan 27, 2011
Posts: 33
|
|
Hi,
you can use String.format("%08d",Integer.parseInt(newstring));
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4165
|
|
Insufficient information. What result do you want if the old String is "00000011"?
please give me if any functions are available to do this.
Java has methods, not functions.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
If you want to do a find & replace from the right of a String, then what you can do is -
reverse your input stringreverse your patterndo a normal replaceFirst or whateverreverse back the replaced string
The above is just a workaround to your problem. I doubt there is a method in java that'd help you do this.
|
OCPJP 6
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
Have you tried a StringBuilder? That sound like what it is designed for.
|
 |
Mahi Ranga
Ranch Hand
Joined: Jan 27, 2011
Posts: 33
|
|
Hi,
you can use like this.
String oldstrring="00000000";
String newstring="1100";
String append=oldstrring+newstring;
String strFormat=String.format("%08d",Integer.parseInt(append));
System.out.println(strFormat);
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
If you simply want to prepend 0s to a String, you can use a loop to append however many 0s to an empty StringBuilder, and append the string you want.
|
 |
jishnu dasgupta
Ranch Hand
Joined: Mar 11, 2011
Posts: 103
|
|
You may also substring the original string and then append the replacement string to it.That i think would be the simplest and mayb the most naive...
|
If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra
|
 |
Alexander Sales
Ranch Hand
Joined: Feb 21, 2011
Posts: 89
|
|
tmpString = oldstring.substring(0,oldstring.length() - newstring.length());
newerString = tmpString + newString;
|
OCPJP 6, OCEWCD Java EE 6
|
 |
Alexander Sales
Ranch Hand
Joined: Feb 21, 2011
Posts: 89
|
|
according to jishnu's idea.
|
 |
 |
|
|
subject: Replacing string
|
|
|