Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching & Replacing Strings

 
Vinod Kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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?
Thanks,
Vinu.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic