Here you can find the source of escapeQuotes(String string)
Parameter | Description |
---|---|
string | String to be processed |
public static String escapeQuotes(String string)
//package com.java2s; /*//www . j a va2 s. c o m * The MIT License (MIT) * <p/> * Copyright (c) 2015 Maksym Dominichenko * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * <p/> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.util.*; public class Main { /** * Replaces in source string all the chars: {@code '}, {@code "} or {@code \} with escaped sequence, accordingly: * {@code \'}, {@code \"} or {@code \\} * * @param string * String to be processed * @return new string with replacements if any */ public static String escapeQuotes(String string) { if (isHollow(string)) return string; return string.replaceAll("(['\"\\\\])", "\\\\$1"); } /** * Helper to check if the String is {@code null} or empty or contains any kind of spaces ("\s" regexp pattern) only. * * @param string * A string to be checked * @return {@code true} if is not {@code null} and contains anything but whitespaces. {@code false} otherwise. */ public static boolean isHollow(String string) { return isEmpty(string) || string.matches("^\\s+$"); } /** * Helper to check if the String is {@code null} or empty.<br/> * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param string * A string to be checked * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise. */ public static boolean isEmpty(String string) { return string == null || string.length() == 0; } /** * Helper to check if the array is {@code null} or empty.<br/> * * @param array * An array to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Object[] array) { return array == null || array.length == 0; } /** * Helper to check if the collection is {@code null} or empty.<br/> * {@link Collection#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param collection * A collection to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Collection<?> collection) { return collection == null || collection.isEmpty(); } /** * Helper to check if the map is {@code null} or empty.<br/> * {@link Map#isEmpty()} is not static and therefore require additional check for {@code null}. * * @param map * A map to be checked * @return {@code true} if is not {@code null} and contains at least one element. {@code false} otherwise. */ public static boolean isEmpty(Map<?, ?> map) { return map == null || map.isEmpty(); } }