Created
October 23, 2013 12:44
-
-
Save sebnilsson/7117980 to your computer and use it in GitHub Desktop.
Trim BOM (Byte Order Mark) from text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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