C # length of a string (in bytes)

Source: Internet
Author: User

I forgot such a simple question. Do you still remember?

 Using  System;  Public   Class  Test {  Public   Static   Void  Main (){  String Str1 = "  Abced...  "  ;  Int Len2 =Str1.length;  Int Leng = System. Text. encoding. Default. getbytes (str1.tochararray (). length; console. writeline (  "  The string is  " + Str1); console. writeline (  "  String Length  " + Len2); console. writeline (  "  Length of bytes  " + Leng); console. Readline ();}} 

The console execution result is

For the length of a byte, one Chinese Character corresponds to two bytes.

 

Take a look at Char nchar varchar nvarchar in SQL Server.

CHAR: it occupies 1 byte for English (ASCII) characters and 2 bytes for a Chinese character. It is convenient to store fixed-length data for char, And the indexing efficiency on the char field is extremely high, for example, if char (10) is defined, whether or not the data you store reaches 10 bytes will occupy 10 bytes of space. Because it is a fixed length, the speed is high.

Varchar: stores variable-length data. If the possible value of a field is not fixed, we only know that it cannot exceed 10 characters and define it as varchar (10) is the most cost-effective. The actual length of the varchar type is the actual length of its value plus 1. Why "+ 1? This byte is used to save the actual length.

 

What is the difference between the nchar and nvarchar types? For conversion from other types of characters, such as Chinese characters and phonetic symbols, each English (ASCII) character occupies two bytes and one Chinese Character occupies two bytes, all characters occupy 2 bytes.

nchar (n): contains Unicode characters of a fixed length of n characters. The value of N must be between 1 and 4,000. The storage size is twice the size of n Bytes.
varchar (n): variable-length character data type. The maximum storage length is 8,000 characters.
nvarchar (n): variable-length Unicode data, it can contain a maximum of 4,000 characters. the storage size of byte is twice the number of input characters, that is, it is two bytes to store data. If data is stored in a single word segment, it also occupies storage space in double bytes.
varchar is generally applicable to English and numbers. nvarchar is applicable to Chinese and other characters, where n represents Unicode constants and can solve the conversion problem between multilingual character sets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.