Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

EscapeSQL in JSP

Posted on 2001-09-07
6
Medium Priority
?
350 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 200 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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