This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String.replaceAll to refer replace content Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "String.replaceAll to refer replace content" Watch "String.replaceAll to refer replace content" New topic
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
    
    4
Well done
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: String.replaceAll to refer replace content
 
Similar Threads
string replacement
Java regex?
Replacing a String
combining characters - \\r\\n to \r\n
Escape sequences in the args[] array