Strip BOM from a File or Text

The following code removes the BOM (Byte Order Mark) from texts. Some text editors add that while saving files. Sometimes this leads to problems while reading or converting text files (e.g. XML files). The code removes the BOM and Zero White Spaces from the string and returns the clean string.

/// <summary>
/// Strips the BOM U+FEFF and ZERO WIDTH SPACE U+200B.
/// </summary>
/// <returns>Clean string</returns>
public static string StripBOM(this string s)
{
  return s.Trim().Trim(new char[] { '\uFEFF', '\u200B' });
}

The method can be used as extension method.

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.