Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String search

 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my jsp page
i have a string whose value which has some special characters what i want is i want to search if there is a special character found in my string and then replace it (there may be sometimes n number of occurences and in any order and i have to replace them ), please tell me how to do it. there is no fixed position for my character inside the string they may appear at any index how can i do it.
i have the idea but not equivalent code ,
while string variable matches special characters then replace(); howcan i do this
 
rom chatterjee
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try converting your String to a StringBuffer. Get the StringBuffer length and iterate through each character, checking for and replacing the special characters. Once you have checked the complete length you can convert it back to a String.
Hope this is what you are looking for
 
Tim Holloway
Saloon Keeper
Posts: 18365
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to http://www.mousetech.com/EJBWizard.html and download a copy of the source code. In file XMLWriter.java you'll see an example where outgoing data is converted to do escapes on the 5 magic HTML characters (& <, &rt; " and &apos . It's pretty simple code with no dependencies on the rest of the system.
I don't remember if it uses StringBuffers or not. It's not used heavily enough to make a difference. The MacroParser module DOES use StringBuffer, but it's a lot harder to pick apart, since its a complex component of a larger and even more complex system.
Of course, if the substitution in question was for the purposes of escaping XML or HTML, you can use XMLWriter "as is".
BTW, this really wasn't the proper place to ask a general Java question, since it has nothing to do with JSP's.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic