File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes duplicating String's replaceAll() method in 1.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "duplicating String Watch "duplicating String New topic
Author

duplicating String's replaceAll() method in 1.3

Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
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

Joined: Mar 13, 2004
Posts: 1272
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/


Mike Gershman
SCJP 1.4, SCWCD in process
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
This regExp package has been around for years and the javadocs are good.
Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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

Joined: Jun 01, 2004
Posts: 47
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

Joined: Jul 30, 2003
Posts: 805
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.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
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

Joined: Jul 30, 2003
Posts: 805
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jun 01, 2004
Posts: 47
SOLUTION:

-Brasskey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: duplicating String's replaceAll() method in 1.3