mum
mum

Reputation: 1645

How check text is 1 byte or 2 byte?

I have a sentence: ex:

 String value="123抽出";

123 is text 1 byte;

抽出 is text 2 byte;

How check text is 1 byte or 2 byte?

Upvotes: 1

Views: 2983

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100527

Exact answer (C# code):

 char c = value[0];
 bool haveHighByteNonEmpty =  (c > 256);

Note that if you want "ASCII" than range is actually different - 0-127, if you want length in some other encoding like UTF8 you should use corresponding methods of Encodings class/instances.

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172408

Strings are unicode and hence are 2 bytes. You may try something like this:

Dim u As System.Text.UnicodeEncoding = System.Text.Encoding.Unicode
Dim a As System.Text.ASCIIEncoding = System.Text.Encoding.ASCII

MsgBox(u.GetByteCount("123"))
MsgBox(s.GetByteCount("123"))

Also check How to check the Single Bytes and Double Bytes character ?

Upvotes: 3

Related Questions