aelena.com

12 July 2009

Escapar texto en formato QuotedPrintable - C#

Filed under: .NET — admin @ 16:35

Una pequeña función que hace uso de las expresiones regulares para escapar (transformar) texto en formato QP y evitar que estas cadenas salgan representadas en pantalla incorrectamente.

La función es muy sencilla:

public string EscapeText(string _textToEscape)
{

    if (!(String.IsNullOrEmpty(_textToEscape)))
    {

        Regex _regex = new Regex(”=[0-9|A-F][0-9|A-F]“);
        MatchCollection _matches = _regex.Matches(_textToEscape);
        foreach (Match _match in _matches)
        {
          int _asciiCode = int.Parse(_match.Value.Substring(1),
            System.Globalization.NumberStyles.HexNumber);
          _textToEscape = _textToEscape.Replace(_match.Value,
          Convert.ToString(Convert.ToChar(_asciiCode)));
         }

         _textToEscape = _textToEscape.Replace(”?=“, ““);
        _textToEscape = _textToEscape.Replace(”  “, “ “);

        return _textToEscape;

    }
    return string.Empty;

}   

Saludos.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment