Java tutorial
//package com.java2s; //License from project: Apache License public class Main { /** * Append string literal in XPath context. * * @param xpath StringBuilder for building XPath. * @param s string literal. */ public static void appendStringLiteral(StringBuilder xpath, String s) { if (s.indexOf('"') >= 0) { if (s.indexOf('\'') >= 0) xpath.append("concat(\"").append(s.replace("\"", "\",'\"',\"")).append("\")"); else xpath.append('\'').append(s).append('\''); } else { xpath.append('"').append(s).append('"'); } } }