Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created October 23, 2013 12:44
Show Gist options
  • Save sebnilsson/7117980 to your computer and use it in GitHub Desktop.
Save sebnilsson/7117980 to your computer and use it in GitHub Desktop.
Trim BOM (Byte Order Mark) from text.
public static string TrimBom(string text, Encoding encoding)
{
var bom = encoding.GetPreamble();
if (bom.Length < 1)
{
return text;
}
var textBytes = Convert.FromBase64String(text);
if (!textBytes.Take(bom.Length).SequenceEqual(bom))
{
return text;
}
var trimmedBytes = textBytes.Skip(bom.Length).ToArray();
return encoding.GetString(trimmedBytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment