string字符串長度和字節長度問題

 
 

string str = "abcdef 安安安";spa

 
 

int i = str.Length;code

 
 

byte[] bt = System.Text.Encoding.Default.GetBytes(str);blog

 
 

int j = bt.Length;內存

 
 

Console.WriteLine($"i={i},j={j}");字符串

 
 

Console.ReadKey();string

 

此時 i  j 分別等於多少呢?it

 

Byte 是字節,一個漢字佔用2個字節。而字符串的length說的是字符串所佔內存空間的長度。class

注意:不管Byte仍是 string ,空格都是佔一個字符的。coding

相關文章
相關標籤/搜索