| Author |
String.replaceAll to refer replace content
|
Sharon whipple
Ranch Hand
Joined: Jul 31, 2003
Posts: 294
|
|
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
Joined: Jan 20, 2010
Posts: 247
|
|
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.
|
Good luck!!
A small leak can sink a Gigantic ship.>
|
 |
Sharon whipple
Ranch Hand
Joined: Jul 31, 2003
Posts: 294
|
|
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
Joined: Jan 20, 2010
Posts: 247
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
Don't use StringTokenizer - use Pattern and Matcher. In pseudo code:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Sharon whipple
Ranch Hand
Joined: Jul 31, 2003
Posts: 294
|
|
Thank you its working great.
>
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
Well done
|
 |
 |
|
|
subject: String.replaceAll to refer replace content
|
|
|