C# String Length
To get the length of a String, use Length property on string.
string.Length
string.Length returns an integer that represents the number of characters in the string.
The Length property includes letters, digits, punctuation marks, spaces, and other characters stored in the string. The first character has index 0, while the last character has index string.Length - 1.
Example – Get String Length in C#
In the following example, we will read a string from console and find its length.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
Console.Write("Enter a string : ");
string str = Console.ReadLine();
int len = str.Length;
Console.WriteLine("Length of the string is : "+len);
}
}
}
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
Enter a string : TutorialKart
Length of the string is : 12
PS D:\workspace\csharp\HelloWorld> dotnet run
Enter a string : Welcome to C# Tutorial.
Length of the string is : 23
In the first run, TutorialKart contains 12 characters. In the second run, the spaces and period are also counted, so the returned length is 23.
C# String Length with Spaces and Punctuation
The Length property does not count only visible letters. Every space and punctuation character stored in the string contributes to the result.
using System;
class Program
{
static void Main()
{
string text = "C# is easy!";
Console.WriteLine(text.Length);
}
}
Output
11
The result includes the letters, the # symbol, two spaces, and the exclamation mark.
Length of an Empty String in C#
An empty string is a valid string that contains no characters. Its length is 0.
using System;
class Program
{
static void Main()
{
string text = "";
Console.WriteLine(text.Length);
}
}
Output
0
An empty string is different from a null string. An empty string refers to a valid String instance, while a null variable does not refer to an instance.
Avoid NullReferenceException When Reading String Length
Accessing Length on a null string throws NullReferenceException. Check the value before reading the property when null is possible.
using System;
class Program
{
static void Main()
{
string text = null;
if (text != null)
{
Console.WriteLine(text.Length);
}
else
{
Console.WriteLine("The string is null.");
}
}
}
Output
The string is null.
The null-conditional and null-coalescing operators provide a shorter way to return zero when the string is null:
string text = null;
int length = text?.Length ?? 0;
Console.WriteLine(length);
Output
0
Check Whether a C# String Is Empty Using Length
You can compare Length with zero to determine whether a non-null string is empty.
string text = "";
if (text.Length == 0)
{
Console.WriteLine("The string is empty.");
}
When the value may be null, string.IsNullOrEmpty() is usually clearer because it handles both null and empty strings.
string text = null;
if (string.IsNullOrEmpty(text))
{
Console.WriteLine("The string is null or empty.");
}
Get the Last Character Using C# String Length
Because string indexes start at zero, the index of the last character is one less than the string length.
using System;
class Program
{
static void Main()
{
string text = "Tutorial";
char lastCharacter = text[text.Length - 1];
Console.WriteLine(lastCharacter);
}
}
Output
l
Check that the string is not empty before using text.Length - 1. Otherwise, the calculated index is -1, which is invalid.
Count Characters After Removing Leading and Trailing Spaces
If leading and trailing spaces should not be included, call Trim() before reading Length.
using System;
class Program
{
static void Main()
{
string text = " C# Tutorial ";
Console.WriteLine(text.Length);
Console.WriteLine(text.Trim().Length);
}
}
Output
14
10
The original string contains two spaces at the beginning and two at the end. Trim() removes those outer spaces before the length is calculated.
C# String Length and Unicode Characters
In .NET, String.Length returns the number of UTF-16 Char values in the string. This usually matches the number of characters a user sees, but some Unicode symbols may use more than one UTF-16 code unit.
using System;
class Program
{
static void Main()
{
string text = "😀";
Console.WriteLine(text.Length);
}
}
Output
2
The emoji appears as one visible symbol but is represented by two UTF-16 code units. For ordinary validation such as checking whether a field is empty or enforcing a simple maximum length, Length is often sufficient. Applications that must count user-perceived Unicode symbols need text-element-aware processing.
C# String Length Questions
Does C# String.Length count spaces?
Yes. Spaces are characters and are included in the value returned by Length.
What is the length of an empty string in C#?
The length of an empty string is 0.
What happens when Length is used on a null string?
Accessing Length on a null string throws NullReferenceException. Check for null or use text?.Length ?? 0 when zero is an acceptable fallback.
Is C# String.Length a method?
No. Length is a read-only property, so it is accessed without parentheses.
Does String.Length always equal the number of visible symbols?
No. It returns the number of UTF-16 Char values. Some emoji and other Unicode symbols occupy more than one Char.
C# String Length Review Checklist
- Use the
Lengthproperty without parentheses. - Remember that spaces and punctuation are included.
- Check for null before accessing
Length. - Check for an empty string before using
Length - 1as an index. - Use
Trim().Lengthonly when outer whitespace should be ignored. - Account for UTF-16 behavior when counting complex Unicode symbols.
C# String Length Summary
In this C# Tutorial, we learned about C# String.Length property with the help of example programs. We also examined empty and null strings, spaces, trimming, string indexes, and Unicode characters.
TutorialKart.com