• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String search

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic