To check if a string is unicode in C# and VB.NET you can use the following snippet.

Sample C#

public static bool IsUnicode(string input)
{
	var asciiBytesCount = Encoding.ASCII.GetByteCount(input);
	var unicodBytesCount = Encoding.UTF8.GetByteCount(input);
	return asciiBytesCount != unicodBytesCount;
}

Sample VB.NET

Public Shared Function IsUnicode(input As String) As Boolean
	Dim asciiBytesCount = Encoding.ASCII.GetByteCount(input)
	Dim unicodBytesCount = Encoding.UTF8.GetByteCount(input)
	Return asciiBytesCount <> unicodBytesCount
End Function

One thought on “How to check if a string is unicode in C# and VB.NET”

Leave a Reply