/// <summary> /// Escapes content so that it is safe for inclusion in a string that will later be used as a /// format string. If this is to be embedded inside of a single-quoted string, be sure to also /// call EscapeSingleQuotedStringContent. /// For example: "'" + EscapeSingleQuotedStringContent(EscapeFormatStringContent(userContent)) + "'" -f $args. /// </summary> /// <param name="value">The content to be included in a format string.</param> /// <returns>Content with all curly braces escaped.</returns> public static string EscapeFormatStringContent(string value) { if (string.IsNullOrEmpty(value)) { return(string.Empty); } StringBuilder sb = new StringBuilder(value.Length); foreach (char c in value) { sb.Append(c); if (CharExtensions.IsCurlyBracket(c)) { // double-up curly brackets to escape them sb.Append(c); } } return(sb.ToString()); }
/// <summary> /// Escapes content so that it is safe for inclusion in a single-quoted string. /// For example: "'" + EscapeSingleQuotedStringContent(userContent) + "'" /// </summary> /// <param name="value">The content to be included in a single-quoted string.</param> /// <returns>Content with all single-quotes escaped.</returns> public static string EscapeSingleQuotedStringContent(string value) { if (string.IsNullOrEmpty(value)) { return(string.Empty); } StringBuilder sb = new StringBuilder(value.Length); foreach (char c in value) { sb.Append(c); if (CharExtensions.IsSingleQuote(c)) { // double-up quotes to escape them sb.Append(c); } } return(sb.ToString()); }