Solved

EscapeSQL in JSP

Posted on 2001-09-07
6
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

710 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