Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

duplicating String's replaceAll() method in 1.3

 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone be kind enough to suggest the most efficient way to duplicate the String class's replaceAll() method?
Unfortunately, our server here at work runs 1.3
Any nudge in the right direction will be greatly appreciated.

-Brasskey
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun added regular expressions to the standard API in 1.4. However, there are independent imlementations around.

Check out this link:
http://www.javaregex.com/
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This regExp package has been around for years and the javadocs are good.
 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, I don't think implementing an outside package will be an option. (Our data security dept. is extremely anal, and it would likely take an act of Congress to get this done.)
I was hoping to somehow duplicate this functionality in 1.3.x using the existing API. Are regular expressions not supported until 1.4?
If this is the case, maybe I am just out of luck here.

What I do currently is this:


I then store this in the db.
To display in my JSP:

Couldn't be simpler. However, I have no earthly idea how to go about doing this without use of the 1.4's replaceAll().
Any help will be GREATLY appreciated

-Brasskey
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't need regular expression matching, you should be able to implement a fairly generalized method to do simple replacements. Take a look at the String API docs. You should pay special attention to the indexOf() and substring() methods.

If you need the full power of regular experssions, you could try to write your own, but this seems like it's more effort than it's worth. Of course, if your place of employment is willing to foot the bill instead of upgrading their software...

Layne
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to use one particular regular expression or are you trying to add regular expressions to the toolkit at your site?
 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not well-versed in the use of regular expressions. Actually, I'm not sure I even need to use a regex. I simply need to replace every occurence of a particular substring ( for example "\r\n" ) within a given String (a very long String ) with a replacement substring.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be a little tedious, but very doable.

I would consider reading the target String into a StringBuffer, then search the StringBuffer for an occurance of the substring (search string). StringBuffer has methods to manipulate and rebuild the target string (lower case here is intentional to distinguish from the String object). Then, create a new String object from the StringBuffer and assign it to the original target String's variable.
 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff,
I'm actually doing something similar to this right now, except using StringTokenizer instead of StringBuffer.

What I'm doing is handing the method a String whose value is derived from a textarea in a JSP.
It replaces "\r" with "&NEWLINE&" correctly.
However, when I use my method to replace "&NEWLINE&" with <br> tags, I run into problems.
For some reason it will look almost right, but some characters will be missing, some breaks will be a bit off.
However, if I use my own replaceAll method to change "\r" to "&LINEFEED&" and use String class's replaceAll method to change "&LINEFEED&s" into <br> tags, it works correctly. I just can't figure out why my method doesn't change "&LINEFEED&" to <br> correctly, but it changes "\r" to "&LINEFEED&" correctly.
[ November 09, 2004: Message edited by: Bill Brasskey ]
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Bill -

For some reason it will look almost right, but some characters will be missing, some breaks will be a bit off.


What characters are missing? How are the breaks off?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the reason to do this in two steps? Can you simply replace "\n\r" directly with "<BR>" rather than using the intermediate "&NEWLINE&"? (Or is it "&LINEFEED&"? I'm confused on this detail.)

Layne
 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOLUTION:

-Brasskey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic