2014-02-21 76 views
1

我有一句話: 例如:如何檢查文本是1字節還是2字節?

String value="123抽出"; 

123是文本1 byte;

​​是文本2 byte;

如何檢查文字是1 byte2 byte

+0

所有的字符串都是Unicode = 2字節。 –

+0

您希望編輯的問題是什麼?你仍然懷疑.net使用UTF-16編碼字符串嗎? –

回答

3

字符串是unicode,因此是2個字節。您可以嘗試這樣的事:

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")) 

還要檢查How to check the Single Bytes and Double Bytes character ?

2

確切的答案(C#代碼):

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

需要注意的是,如果你想 「ASCII」 比範圍實際上是不同的 - 0 -127,如果你想在UTF8等其他編碼中使用長度,你應該使用Encodings類/實例的相應方法。

相關問題