In C#, a String is an array of characters. The string class represents the text as a series of Unicode characters. It provides various properties and methods so that it becomes easy to work with strings. There are two properties in the 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.
Example: Printing the characters of string using the string class properties.
C#
using System;
class Geeks
{
public static void Main(String[] args)
{
String s = "Geeks";
// Displaying the string
// Length property to get the length of the string
int length = s.Length;
for (int i = 0; i < length; i++)
// Displaying the characters of the string
// Using Chars property
Console.WriteLine($"Character at index {i} is: '{s[i]}'");
}
}
OutputCharacter at index 0 is: 'G'
Character at index 1 is: 'e'
Character at index 2 is: 'e'
Character at index 3 is: 'k'
Character at index 4 is: 's'
Explanation: In the above example, we use the Length property of string class which returns the number of character present in the specified string and we get the character at that index using the chars property.
1. Chars(int 32)
Chars[Int32] is used to retrieve a character at a specific index in the string. For example, if we want to access the first character of the string s = "Geeks". So we can access it by using the s[0] it will return a character of the string which is present at the first index which 'G'.
Syntax:
public char this[int index] { get; }
- Parameter: It takes a single parameter index( zero-based) which is the position in the current string of type System.Int32.
- Return Type: Returns the Char object at the position specified by the index parameter and Its property value type is System.Char.
Example 1: Accessing characters of a string using the Chars property.
C#
// C# program to demonstrate the
// Chars property of String class
using System;
class Geeks
{
public static void Main()
{
// String Declaration
string s = "GeeksforGeeks";
// Display each character present in the
// String using Chars property
for (int i = 0; i < s.Length; i++) // Changed <= to <
{
Console.Write("{0} ", s[i]);
}
}
}
OutputG e e k s f o r G e e k s
Example 2: Validating whether the character is a number or not using the Chars(Indexers) property.
C#
// C# program to check whether
// a character is a number or a letter
using System;
class Geeks
{
public static void Main()
{
string s = "3G5ee7K";
for (int i = 0; i < s.Length; i++)
{
// Access Characters using Chars(Indexers) Property
if (Char.IsDigit(s[i]))
Console.WriteLine("{0} is a Number.", s[i]);
else
Console.WriteLine("{0} is a character.", s[i]);
}
}
}
Output3 is a Number.
G is a character.
5 is a Number.
e is a character.
e is a character.
7 is a Number.
K is a character.
2. 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.
public int Length {
get;
}
Return Value: It returns the number of Char objects in this instance of String.
Note: C/C++ strings are null-terminated, but C# strings can contain null characters. These nulls count towards the string's length. For example, "xyz\0abc" has a Length of 7.
Example 1: Using the length property to count the number of characters present in the string(including null values).
C#
// C# program to demonstrate the
// Length property and null characters
using System;
class Geeks
{
public static void Main()
{
// Include four null characters between "xyz" and "abc"
string str = "xyz\0\0\0\0abc";
// Print length including null characters
Console.WriteLine(str.Length);
}
}
Example 2: C# program to get the count of characters in a string using the Length property.
C#
// C# program to illustrate the
// Length property of String class
using System;
class Geeks
{
public static void Main()
{
// Taking the string variable
// and then finding length
string s = "GeeksforGeeks";
Console.WriteLine("'{0}' length : {1}", s, s.Length);
// Directly use Length property
Console.WriteLine("'{0}' length : {1}", "GEEKS", "GEEKS".Length);
// Store the length in an integer variable
int l = s.Length;
Console.WriteLine("'{0}' length : {1}", s, l);
}
}
Output'GeeksforGeeks' length : 13
'GEEKS' length : 5
'GeeksforGeeks' length : 13