Skip to content

Escape Method ​

Definition ​

  • Namespace: JASE.Extensions
  • Assembly: JASE.Primitives.dll

string Escape(string input, char charToEscape, char escapeChar) ​

csharp
string Escape(string input, char charToEscape, char escapeChar)

Inserts an escape character before each occurrence of a specified character in the input string, unless that occurrence is already escaped.

Parameters ​

NameDescription
charToEscapeThe character that should be escaped wherever it appears.
escapeCharThe escape character to insert before charToEscape when it is not already escaped.
inputThe source string to scan. If or empty, the method returns it unchanged.

Returns ​

A new string where all unescaped occurrences of charToEscape have been prefixed with escapeChar.

Remarks ​

An occurrence of charToEscape is considered "already escaped" if it is immediately preceded by escapeChar. In that case, no additional escape character is inserted.

This implementation performs a single pass over the input without recursion and avoids repeated substring allocations used in the legacy version.