Lucene.Net.QueryParsers.QueryParser.Escape C# (CSharp) Method

Escape() public static method

Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.
public static Escape ( String s ) : String
s String
return String
        public static String Escape(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                // These characters are part of the query syntax and must be escaped
                if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
                    || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
                    || c == '*' || c == '?' || c == '|' || c == '&')
                {
                    sb.Append('\\');
                }
                sb.Append(c);
            }
            return sb.ToString();
        }