Search code examples
.netencodingfilestreamiostreamstreamreader

How to get the encoding type of a string?


With the following code, I get a string from a stream but I don't know the encoding type because it's detected automatically by StreamReader;

how can I get the encoding type of the string respHTML?

Dim reader As StreamReader = New StreamReader(respStream, True)
Dim respHTML as String = reader.ReadToEnd()

Solution

  • Strings contain Unicode characters, not bytes.
    Encodings are ways to save Unicode characters as bytes; a string doesn't have any encoding.

    You can get the encoding used by the StreamReader by checking the CurrentEncoding property.