I copied blindly one of the stackoverflow answers, but it didn't work for me as I expected. I needed to remove a UTF8 byte order mark from a string, and somehow, the inputText.StartsWith(byteOrderMark) always returns true for whatever string:
internal class Program
{
static void Main(string[] args)
{
var inputText = "hello";
string byteOrderMark = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (inputText.StartsWith(byteOrderMark))
inputText = inputText.Remove(0, byteOrderMark.Length);
Console.WriteLine(inputText); // ello
Console.WriteLine(inputText[0] == byteOrderMark[0]); // false
}
}
I can check it character by character, there is no problem. I'm interested why StartsWith returns true even when the string doesn't start with UTF8 preamble?