Java String Quote quote(String p_sql)

Here you can find the source of quote(String p_sql)

Description

quote

License

Apache License

Declaration

static public String quote(String p_sql) 

Method Source Code

//package com.java2s;
/**//from  w  w  w  .  j  a v  a2 s  . c om
 *  Copyright 2009 Welocalize, Inc. 
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

public class Main {
    static public String quote(String p_sql) {
        String singleQuote = "'";
        String sql = singleQuote;
        int index = p_sql.indexOf(singleQuote);

        // Is there a single quote in the string      

        while (index != -1) {
            // yes, copy the sub-string
            sql += p_sql.substring(0, index + 1);
            // add an extra quote - escape character
            sql += singleQuote;
            // are there more characters
            if (p_sql.length() > index + 1) {
                // yes
                p_sql = p_sql.substring(index + 1);
                index = p_sql.indexOf(singleQuote);
            } else {
                // no, exit the while loop - we're finished
                index = -1;
                p_sql = "";
            }
        }

        sql += p_sql;

        // add the end quote
        sql += singleQuote;
        return sql;
    }
}

Related

  1. quote(String message, String quotationMark)
  2. quote(String name)
  3. quote(String name)
  4. quote(String name)
  5. quote(String name)
  6. quote(String p_string)
  7. quote(String path)
  8. quote(String s)
  9. quote(String s)