C# String Properties

The String class in C# provides essential properties that allow you to access and examine string data. The two primary properties are Chars for accessing individual characters and Length for determining the string size.

String Properties

Following are the key properties of the String class in C# −

Property Description
Chars Gets the Char object at a specified position in the current String object using indexer syntax.
Length Gets the number of characters in the current String object.

Syntax

Following is the syntax for accessing string properties −

// Accessing character at index
char character = stringName[index];

// Getting string length
int length = stringName.Length;

Using Length and Chars Properties

Example

using System;

public class Demo {
    public static void Main() {
        string str1 = "h8b9";
        string str2 = "abcdef";
        
        Console.WriteLine("String1: " + str1);
        Console.WriteLine("String1 length: " + str1.Length);
        Console.WriteLine("String2: " + str2);
        Console.WriteLine("String2 length: " + str2.Length);
        
        Console.WriteLine("\nAnalyzing characters in str1:");
        for (int i = 0; i < str1.Length; i++) {
            if (Char.IsLetter(str1[i]))
                Console.WriteLine("'" + str1[i] + "' at index " + i + " is a letter");
            else
                Console.WriteLine("'" + str1[i] + "' at index " + i + " is a number");
        }
    }
}

The output of the above code is −

String1: h8b9
String1 length: 4
String2: abcdef
String2 length: 6

Analyzing characters in str1:
'h' at index 0 is a letter
'8' at index 1 is a number
'b' at index 2 is a letter
'9' at index 3 is a number

Working with Empty and Null Strings

Example

using System;

public class Demo {
    public static void Main() {
        string str1 = "Hello World";
        string str2 = String.Empty;
        string str3 = "   ";
        
        Console.WriteLine("String1: '" + str1 + "'");
        Console.WriteLine("Length: " + str1.Length);
        Console.WriteLine("First character: " + str1[0]);
        Console.WriteLine("Last character: " + str1[str1.Length - 1]);
        
        Console.WriteLine("\nString2 (Empty): '" + str2 + "'");
        Console.WriteLine("Length: " + str2.Length);
        Console.WriteLine("Is null or empty: " + String.IsNullOrEmpty(str2));
        
        Console.WriteLine("\nString3 (Whitespace): '" + str3 + "'");
        Console.WriteLine("Length: " + str3.Length);
        Console.WriteLine("Is null or whitespace: " + String.IsNullOrWhiteSpace(str3));
    }
}

The output of the above code is −

String1: 'Hello World'
Length: 11
First character: H
Last character: d

String2 (Empty): ''
Length: 0
Is null or empty: True

String3 (Whitespace): '   '
Length: 3
Is null or whitespace: True

Practical String Property Usage

Example

using System;

public class Demo {
    public static void Main() {
        string text = "Programming";
        
        Console.WriteLine("Original string: " + text);
        Console.WriteLine("Length: " + text.Length);
        
        // Reverse the string using Chars property
        Console.Write("Reversed: ");
        for (int i = text.Length - 1; i >= 0; i--) {
            Console.Write(text[i]);
        }
        Console.WriteLine();
        
        // Count vowels using Chars property
        int vowelCount = 0;
        string vowels = "aeiouAEIOU";
        
        for (int i = 0; i < text.Length; i++) {
            if (vowels.Contains(text[i].ToString())) {
                vowelCount++;
            }
        }
        Console.WriteLine("Number of vowels: " + vowelCount);
    }
}

The output of the above code is −

Original string: Programming
Length: 11
Reversed: gnimmargorP
Number of vowels: 3

Conclusion

The Length property returns the number of characters in a string, while the Chars property (accessed via indexer) allows you to retrieve individual characters at specific positions. These properties are fundamental for string manipulation, validation, and character-level processing in C# applications.

Updated on: 2026-03-17T07:04:36+05:30

929 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements