• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need to replace all integers in a string with new random integers , java newbie and struggling with  RSS feed

 
Barb Switzer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ex:

String uuid = "LFD69EED05F011E683B38B7A62A9F7BEG";

Need to randomize just the integers in the above string (replace with new random integers)..

Any help would be appreciated.

Played with .replace and .replaceall with little success.

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to do this?

What did you try with replaceAll() that didn't work?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can build a regex and match the string with this regex,whenever it finds a match replace the match with any random integer.take a pen and paper,try to build the logic accordingly with i have mentioned,if you find things difficult at some point don't hesitate to put a reply then.

kind regards,
praveen.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In pseudo code
Convert given string to char[]
Iterate each
if char is a number, replace value in char array (simple substitution lile 1=A,2=B or whatever the requirement is)
Once done reconvert/process char[]
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have just tried another method using concatenation its working you can use it
DEMO:-

 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a poor solution Praveen. It will only solve the problem for one specific case, and it's very verbose.

you should be able to implement a method like this in one line of no more than 80 characters:
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:That's a poor solution Praveen. It will only solve the problem for one specific case, and it's very verbose.

You should be able to implement a method like this in one line of no more than 80 characters:


I am thinking the same thing thanks for making me sure stephan,even it needs lots of effort if string grows its length  somewhat around 100,1000 or more.i think using regex will be far better than this which i adviced initially.

Thanks! again Stephan for your opinion.

kind regards,
praveen.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, disregard the one line part in my last post. I made a big mistake in my reasoning. The best way to solve this problem is probably what Maneesh proposed.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:In pseudo code
Convert given string to char[]
. . .
Convert the String to a StringBuilder surely?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the new digit be the same as the old digit?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!