C# String Properties

Learn via video courses
Topics Covered

Overview

Strings are an array of characters and are a fundamental part of any programming language that enables developers to manipulate and manage textual data effectively. Strings play a crucial part in software development in C# and contain a variety of characteristics that go beyond the fundamental string manipulation and give increased functionality and flexibility to perform various operations.

Introduction to String Properties

A string in C# is a collection of characters that are represented by the string data type. Strings are immutable, which means that once they have been produced, their contents cannot be directly modified. This immutability makes memory management easier and guarantees the security of data. Here is a straightforward C# example of declaring a string:

In the above example, we have a string variable x that stores the "Welcome" string.

Types of String Properties

There are two properties of the string class which are as follows:

String.Chars Property

It is a property that allows us to extract the character present at a particular index in a string. In C#, the Chars property is used using indexes.

It takes an integer value of type System.Int32 which is the index of the particular character that needs to be extracted from the string. Moreover, the return type of this property is System.Char which returns a character.

Below is a code example to understand the Chars property in a better way.

Example:

Output:

In the above example, we have stored a string "HelloWorld" in the s variable. Now, we iterate through the string and get the characters of the string one by one by using the looping variable i as the index of the character that needs to be extracted.

String.Length Property

The String.Length property is used to find the number of characters in the string object. Therefore, it finds out the length of the entire string.

Now, let us look at a code example of understanding the String.Length property of strings.

Example:

Output:

In the above example, we have to find the length of the characters present in the string.

Note: In other programming languages like C or C++, a null character is used to depict the end of the string. However, in C# the null character is embedded into the string. An example can be that there are strings Hello and World. If we combine both these strings using a null character then the length of the string "Hello\0World" is 11 instead of 10.

A code example is also given for a better understanding.

Output:

As you can see in the above example, we have embedded two null characters into the string s, therefore the length of the string becomes 12.

Conclusion

  • A C# String refers to an array of characters.
  • C# string has two properties chars[Int32] and the Length property.
  • The chars[Int32] property extracts the character at a particular index.
  • The Length C# string property is used to find the length of the characters present in a string.