Open In App

C# | String Properties

The string is an array of characters. String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of String class is to provide the properties and methods so that it becomes easy to work with strings.

There are the two properties of String class :



  1. Chars[Int32]: Used to get the Char object at a specified position in the current String object. In C#, the Chars property is an indexer.
  2. Length: Used to get the number of characters in the current String object.

String.Chars Property (Int32)

Syntax:



public char this[int index] 
{ 
      get; 
}

Below are the programs to illustrate the Chars Property:

String.Length Property

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

Syntax:

public int Length 
{ 
    get;
}

Note: In C and C++, a null character indicates the end of a string but in C#, a null character can be embedded in a string. When a string includes one or more null characters, then it also considers in the total length of that string. For example, If in a string, the substrings “xyz” and “abc” are separated by a null character like as String value is “xyz\0abc”, Then Length property returns 7, where it includes the six alphabetic characters as well as the null character.

Below are the programs to illustrate the Length Property:

References:


Article Tags :
C#