Finding the length of a string

C#:
Finding the length of a string

How to:

In C#, the string.Length property gives you the number of characters in a string. Here’s how to use it:

using System;

class Program
{
    static void Main()
    {
        string example = "Hello, World!";
        Console.WriteLine(example.Length); // Output: 13
    }
}

Easy, right? But remember, it counts characters, not bytes. With emojis or special characters, things can get tricky. More on that later.

Deep Dive

Historically, finding the length of a string was tied to memory management and manipulation in programming. Since C# is a high-level language, it abstracts that low-level work away. Still, it’s good to know what’s under the hood.

Alternatives? Sure! You might see example.ToCharArray().Length out in the wild, but it’s just doing extra legwork for the same result.

Now, about those tricky characters. C#’s Length property counts a string’s char objects, each representing a UTF-16 code unit. That’s fine until you encounter surrogate pairs – characters like emojis that need two char objects. Here’s the thing: Length counts those as two. Yep.

For an accurate count of visual characters or grapheme clusters, you’d need System.Globalization’s StringInfo class:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string example = "👍"; // Thumbs up emoji

        Console.WriteLine(example.Length); // Output: 2 <- Because of the surrogate pair!
        Console.WriteLine(new StringInfo(example).LengthInTextElements); // Output: 1
    }
}

Understand the difference? It’s not just academic; it could affect text processing in meaningful ways.

See Also

Explore more with these resources:

Know your strings, handle them wisely, and write code that counts – in every sense.