File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Searching & Replacing Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching & Replacing Strings" Watch "Searching & Replacing Strings" New topic

Searching & Replacing Strings

Vinod Kumar

Joined: Jan 22, 2002
Posts: 10
I am new to searching strings so can someone please explain how to do this? I have an XML file with characters such as [%, ^, &, $, #, !] that I want to replace with a space. How would I do that? When executing the application, the user will specify an argument like this:
java SearchMe Hello%Vacation!
And the program should replace the %, and ! with a space and output like this:
Hello Vacation
Also, is the XML approach good or bad?
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
This is from the String class definition in The JavaTM 2 Platform, Standard Edition, v 1.3.1 - API Specification.

public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Note: This method is available to String objects. So...
String newString = oldString.replace(oldChar, newChar);
Good Luck,
-Dirk Schreckmann
[ February 06, 2002: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Searching & Replacing Strings
It's not a secret anymore!