• Post Reply Bookmark Topic Watch Topic
  • New Topic

String.replaceAll to refer replace content  RSS feed

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to replace all numbers in a string to new lines according to the number

For example:
String a = " this 2 is 4 my 3 string";
Output will be "
String a = " this \n\n is \n\n\n\n my \n\n\n string";

Is it possible to do that using string.replaceAll or i need to tokenize the string build a new one?

Thank you
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sharon..

Of course it is possible to replace all numbers to new Line .
ReplaceAll takes Regex patter and the new String for replacement.

It can be like that..


Regards.
Raza.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raza,

I think i didn't explain correctly
The replacement should plant new lines according to the content being replaced

For example:
If it finds 2 - > replace it with 2 new lines (\n\n)
If it finds 10 - > replace it with 10 new lines (\n\n\n\n\n\n\n\n\n\n)
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops i did not anticipated correctly.

I dont think that replaceAll method will allow to do that.You will have to use StringTokenizer for that.

Regards.
Raza



 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringTokenizer - use Pattern and Matcher. In pseudo code:
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you its working great.



>
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!