Solved

EscapeSQL in JSP

Posted on 2001-09-07
6
317 Views
Last Modified: 2010-05-18
Hi i want to replace ' with '' in JSP. Is there any equivalent of VB Replace() function in Java
0
Comment
Question by:Misbah
6 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6465196
I'm not sure whether I'm answering your question or not ...

The String class has a replace method that will replace all occurrences of a single character with another character and return the resulting String object. But neither String nor StringBuffer have methods that allow all occurrences of a substring to be replaced with another substring. Here is a fairly efficient implementation that you might try. First argument is source string, second is the search substring, third is the replacement substring.

   public static String replaceSubstring(String str, String pattern, String replace) {
       int slen = str.length();
       int plen = pattern.length();
       int s = 0, e = 0;
       StringBuffer result = new StringBuffer(slen);
       char[] chars = new char[slen];

       while ((e = str.indexOf(pattern, s)) >= 0) {
           str.getChars(s, e, chars, 0);
           result.append(chars, 0, e - s).append(replace);
           s = e + plen;
       }
       str.getChars(s, slen, chars, 0);
       result.append(chars, 0, slen - s);
       return result.toString();
   }

Best regards,
Jim Cakalic
0
 
LVL 1

Expert Comment

by:kanthonym
ID: 6465844
Or if you are familiar with Perl, and are able to include the PerlTools.jar on your classpath this an be executed much faster and with less code using regular expression.
0
 
LVL 92

Expert Comment

by:objects
ID: 6468199
> and with less code ...

Depends if you count the additional code downloaded in PerlTools.jar.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 8908501
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 8916615
OK. Thanks. :-)
Jim
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java. Convert method from recursion based to iteration based ( loop based ) 6 79
Java Loop 6 50
split string containing \r\n in Java 46 38
Java SE 8u111  Lot of stuff broke 11 54
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now