Solved

EscapeSQL in JSP

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 62
hibernate example for saving data 19 37
Android development question 2 35
JavaFX TableView not displaying correctly 3 16
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

775 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