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.
Two options:
Include it as a character in your StringBuilder
:
sb.Append('\uFEFF'); // U+FEFF is the byte-order mark character
Personally I'd go for the first approach normally, but the "I can't modify that method" suggests it may not be an option in your case.