Solved

EscapeSQL in JSP

Posted on 2001-09-07
6
311 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

747 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

11 Experts available now in Live!

Get 1:1 Help Now