File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you simulate String.replace(...) & String.contains(...)

 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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?

Thanks..
Majid
[ August 29, 2006: Message edited by: Majid Al-Fifi ]
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*** replace(String,String)

You could write a regular expression and use String.replaceFirst()

*** contains(String)

and again you could write a regular expression and use String.matches() .

The detail of the regular expressions to use depends on the characters to be replaced/matched.
 
Alan Moore
Ranch Hand
Posts: 262
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

I have used used the two methods Alan provided and everything is fine.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic