public static String writeQuotedString(String unquotedString, bool optional, String tag = null) { if (unquotedString != null || !optional) { StringBuilder builder = new StringBuilder(unquotedString.Length + 2); builder.Append("\""); for (int i = 0; i < unquotedString.Length; ++i) { char c = unquotedString[i]; if (i == 0 && ParseUtil.isWhitespace(c)) { throw new ParseException(ParseExceptionType.ILLEGAL_WHITESPACE, tag); } else if (c == '"') { builder.Append('\\').Append(c); } else { builder.Append(c); } } builder.Append("\""); return(builder.ToString()); } return("\"\""); }