TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# String Length Property: Get Character Count

Use the Length property on the string type. Length gets the character count.
Length. Every string object has a Length property. Every character (no matter its value) is counted in this property. Length is cached on strings.
Length, notes. Length gets the character count in the string instance. The string cannot be null. It is possible to avoid exceptions with help from Length.Null
An example. The string class has a Length property, which returns the number of characters in its internal buffer. You do not need to iterate through every character of a string.

Here: The 4 strings we use are all of different Lengths. There are variable strings, constant strings, and literal strings.

Final part: This treats a string literal, contained in quotation marks, as an object in the C# language.

String Literal
C# program that uses Length using System; class Program { static void Main() { // An example string. string a = "One example"; Console.WriteLine("LENGTH: " + a.Length); // An empty example string. string b = ""; Console.WriteLine("LENGTH: " + b.Length); // A constant string. const string c = "Three"; Console.WriteLine("LENGTH: " + c.Length); // A literal string. Console.WriteLine("LENGTH: " + "Four".Length); } } Output LENGTH: 11 LENGTH: 0 LENGTH: 5 LENGTH: 4
Null. We must first test for null strings before calling the Length property. This is because you cannot access members on null reference types. The IsNullOrEmpty method is ideal for this.IsNullOrEmpty, IsNullOrWhiteSpace

Also: There are some interesting behaviors with null strings at the class level.

Null Strings
C# program that deals with null Length using System; class Program { static void Main() { F(null); F("cat"); F("book"); F(""); } static void F(string a) { if (string.IsNullOrEmpty(a)) { // String is null or empty. Console.WriteLine(true); } else { // Print length of string. Console.WriteLine(a.Length); } } } Output True 3 4 True
Error, cannot be assigned. We cannot assign to Length. We can only read the value of Length and change the string reference in other ways. This is an important aspect of the C# string type.
C# program that causes assigned error class Program { static void Main() { string value = "test"; value.Length = 10; } } Output error CS0200: Property or indexer 'string.Length' cannot be assigned to -- it is read only
Research. Microsoft's article on this subject has some interesting details. It tells you that the Length is not derived from the number of chars, but not symbolic Unicode chars.

Tip: It is possible for null characters to be in the middle of C# strings. But this is not a common occurrence in purely managed code.

Quote: The Length property returns the number of Char objects in this instance, not the number of Unicode characters. The reason is that a Unicode character might be represented by more than one Char.

String.Length Property: Microsoft Docs
Callvirt. When accessing string Length, the callvirt MSIL instruction will be generated. This will be slower than using a constant integer.IL: callvirt
Performance, hoisting length. In earlier versions of the .NET Framework, hoisting the Length and using a local helped speedup programs.

But: In 2019, this optimization seems to have lost its effectiveness. The JIT compiler inlines Length accesses well.

Opinion: It is probably worth storing the Length of a string in a local if you use it many times, but do not expect a huge performance win.

A summary. The Length property finds the number of chars in a string. It is precomputed, so is fast to access repeatedly. It returns its result in constant time.Property
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf