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 :
- 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.
- Length: Used to get the number of characters in the current String object.
String.Chars Property (Int32)
Syntax:
public char this[int index]
{
get;
}
- Parameters: This method only take one parameter i.e. index which is the position in the current string and its type is System.Int32. The index parameter is zero-based.
- Return Value: This method returns the Char object at the position specified by the index parameter and Its property value type is System.Char.
Below are the programs to illustrate the Chars Property:
- Program 1: Each Char object in the string can be accessed by using code such as the following. .
// C# program to demonstrate the// Chars property of String classusingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){stringstr ="GeeksforGeeks";for(inti = 0; i <= str.Length - 1; i++ )Console.Write("{0} ", str[i]);}}chevron_rightfilter_noneOutput:G e e k s f o r G e e k s
- Program 2: The use of this indexer in a routine to validate a string.
// C# program to check the whether the// character is number or characterusingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){stringstr ="11Gee45for78geeks";for(inti = 0; i < str.Length; i++){if(Char.IsDigit(str[i]))Console.WriteLine("{0} is a Number.", str[i]);elseConsole.WriteLine("{0} is a character.", str[i]);}}}chevron_rightfilter_noneOutput:1 is a Number. 1 is a Number. G is a character. e is a character. e is a character. 4 is a Number. 5 is a Number. f is a character. o is a character. r is a character. 7 is a Number. 8 is a Number. g is a character. e is a character. e is a character. k is a character. s is a character.
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;
}
- Return Value: It returns the number of Char objects in this instance of String.
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:
- Program 1:
// C# program to demonstrate the// Length propertyusingSystem;classGeeks{// Main MethodpublicstaticvoidMain(){// here include four null character// between xyz and abc substringstringstr ="xyz\0\0\0\0abc";Console.WriteLine(str.Length);}}chevron_rightfilter_noneOutput:10
- Program 2:
// C# program to illustrate the// Length property of String classusingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){// taking the string variable// and then find lengthstringstr ="GeeksforGeeks";Console.WriteLine("'{0}' length : {1}", str, str.Length);// Directly use length property// with string here no variable useConsole.WriteLine("'{0}' length : {1}","GEEKS","GEEKS".Length);// return value stored the in// integer variable lintl = str.Length;Console.WriteLine("'{0}' length : {1}", str, l);}}chevron_rightfilter_noneOutput:'GeeksforGeeks' length : 13 'GEEKS' length : 5 'GeeksforGeeks' length : 13
References:
- https://msdn.microsoft.com/en-us/library/system.string.chars
- https://msdn.microsoft.com/en-us/library/system.string.length
Recommended Posts:
- C# | Properties
- C# | Restrictions on Properties
- C# | Equals(String, String) Method
- C# | How to copy a String into another String
- C# | String
- C# | String vs StringBuilder
- C# | String Operators
- C# | Add a string to the end of the StringCollection
- C# | String.Contains() Method
- C# | How to get the HashCode for the string
- C# | String class
- C# | Check if two String objects have the same value | Set-1
- C# | Check if the specified string is in the StringCollection
- C# | String.IndexOf( ) Method | Set - 1
- C# | String Concat with examples | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



