• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Replacing string

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you can use String.format("%08d",Integer.parseInt(newstring));
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Marshal
    Posts: 76885
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 76885
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 103
    Netbeans IDE Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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...
     
    Ranch Hand
    Posts: 89
    Eclipse IDE Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    tmpString = oldstring.substring(0,oldstring.length() - newstring.length());
    newerString = tmpString + newString;
     
    Alexander Sales
    Ranch Hand
    Posts: 89
    Eclipse IDE Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    according to jishnu's idea.
     
    We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic