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
Similar Reads
C# Properties
Properties are the special types of class members that provide a flexible mechanism to read, write, or compute the value of a private field. Properties function like public data members but are accessors which makes easy data access. Properties also support encapsulation and abstraction through "get
4 min read
std::string::compare() in C++
The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
4 min read
Strings in C
A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
std::string::insert() in C++
In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
std::string::data() in C++
The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po
2 min read
StringBuilder.Chars[] Property in C#
StringBuilder.Chars[Int32] Property is used to get or set the character at the specified character position in this instance. Syntax: public char this[int index] { get; set; } Here, the index is the position of the character. Property Value: This property returns the Unicode character at position in
2 min read
C# String Class
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. The array of characters is also termed as the text. So the string is the representation of the text. A string is represented by a class System.String. The String c
9 min read
std::basic_string::operator[] in C++
Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined. Syntax : reference operator[] (size_type pos); const_reference operator[] (size_type pos) const; Parameters : pos - position of the character to return Retu
1 min read
std::string::append() in C++
The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class .The string::append() function can be used to do the following append operations:Table of ContentAppend a Whole StringAppend a Part of the StringAppend a C
3 min read
Practice questions on Strings
String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = âstringâ), both of which can be access
4 min read