As you know jdk1.5 String class provides *** replace(String,String) and *** contains(String)
I have developed an app that uses those methods to discover that I need to compile my app using jdk1.4 because of some restrictions on the production environment.
I am thinking of making MyStringUtil.java and make the method as follows:
public static String replace(String str, String s1, String s2) { //find all s1's in str and replace them with s2's //return str }
Does anyone have code for an algorithm that does their job exactly. Is there a better alternative?
For the contains() method, I would just do it the way Sun did: (That method has always seemed like a waste of space to me.) As for replace(), I would use replaceAll() behind the scenes: Having to pre-process the strings seems inefficient, but (for longer target strings, at least) that will be offset by the fact that the regex package will use a Boyer-Moore algorithm to do the searching.
Majid Al-Fifi
Ranch Hand
Joined: Aug 22, 2006
Posts: 45
posted
0
Thanks guys,
I have used used the two methods Alan provided and everything is fine.