• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing string

 
sankar dunga
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can use String.format("%08d",Integer.parseInt(newstring));
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to do a find & replace from the right of a String, then what you can do is -
  • reverse your input string
  • reverse your pattern
  • do a normal replaceFirst or whatever
  • reverse 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.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49823
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have you tried a StringBuilder? That sound like what it is designed for.
     
    Mahi Ranga
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49823
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 103
    Eclipse IDE Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    Alexander Sales
    Ranch Hand
    Posts: 89
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    tmpString = oldstring.substring(0,oldstring.length() - newstring.length());
    newerString = tmpString + newString;
     
    Alexander Sales
    Ranch Hand
    Posts: 89
    Eclipse IDE Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    according to jishnu's idea.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic