How can I add a byte order mark to a StringBuilder? (I have to pass a string to another method which will save it as a file, but I can't modify that method).
I tried this:
var sb = new StringBuilder();
sb.Append('\xEF');
sb.Append('\xBB');
sb.Append('\xBF');
But when I view it with hex editor, it adds the following sequence:
C3 AF C2 BB C2 BF
The string is huge, so it would be good to do it without back and forth converting to byte array.
Edit: Clarification after questions in comments. I have to pass the string to another method which takes a string and creates a file of it on Azure Blob Storage. I can't modify the other method.
File.WriteAllText()
- it supports setting the encoding to UTF8 which will add a BOM. See: learn.microsoft.com/en-us/dotnet/api/… – Zebulun