C# String - GetEnumerator() Method



The C# String GetEnumerator() method is used to retrieves an enumerator or object that can iterate through the individual characters in this string.

Syntax

Following is the syntax of the C# string GetEnumerator() method −

public CharEnumerator GetEnumerator ();

Parameters

This method does not accepts any parameters.

Return value

This method returns an enumerator object that can be used to iterate through the characters of the string.

Example 1: Basic String GetEnumerator

Following is the basic example of theGetEnumerator()method to get all the character of the string −

using System;
class Program {
   static void Main() {
      string sample = "tutorialspoint, ";
      CharEnumerator enumerator = sample.GetEnumerator();

      while (enumerator.MoveNext())
      {
         char currentChar = enumerator.Current;
         Console.WriteLine(currentChar);
      }
   }
}

Output

Following is the output −

t
u
t
o
r
i
a
l
s
p
o
i
n
t
,

Example 2: Counting Vowel In a String

Let us look at another example of the GetEnumerator() method to get the enumerator for the string. We then iterate through each character to check if the character is a vowel or not −

using System;
class Program {
   static void Main() {
      string input = "Learning c# is fun!";
      int vowelCount = 0;

      // Get the enumerator for the string
      CharEnumerator enumerator = input.GetEnumerator();

      // Iterate through each character in the string
      while (enumerator.MoveNext()) {
         char currentChar = char.ToLower(enumerator.Current);

         // Check if the character is a vowel 
         if ("aeiou".Contains(currentChar)) {
            vowelCount++;
         }
      }
      Console.WriteLine($"The number of vowels in the string is: {vowelCount}");
   }
}

Output

Following is the output −

The number of vowels in the string is: 5

Example 3: Reversing a String

In this example, we use the GetEnumerator() method to demonstrating how it can be used to reverse a string by explicitly iterating through its characters −

using System;
class Program {
   static void Main() {
      string original = "Hello, tutorialspoint";
      string reversed = ReverseString(original);

      Console.WriteLine("Original String: " + original);
      Console.WriteLine("Reversed String: " + reversed);
   }

   static string ReverseString(string input) {
      CharEnumerator enumerator = input.GetEnumerator();
      string reversed = string.Empty;

      // Iterate through the string using the enumerator
      while (enumerator.MoveNext()) {
         reversed = enumerator.Current + reversed;
      }
      return reversed;
   }
}

Output

Following is the output −

Original String: Hello, World!
Reversed String: !dlroW ,olleH

Example 4: Counting Consonant In a String

The below example uses the GetEnumerator() method to get the enumerator for the string. We then iterate through each character to check if the character is a constant or not −

using System;
class Program {
   static void Main() {
      string input = "Learning c# is fun!";
      int consonantCount = 0;

      // Get the enumerator for the string
      CharEnumerator enumerator = input.GetEnumerator();

      // Iterate through each character in the string
      while (enumerator.MoveNext()) {
         char currentChar = char.ToLower(enumerator.Current);

         // Check if the character is a vowel
         if (char.IsLetter(currentChar) && !"aeiou".Contains(currentChar)) {
            consonantCount++;
         }
      }
      Console.WriteLine($"The number of consonants in the string is: {consonantCount}");
   }
}

Output

Following is the output −

The number of consonants in the string is: 9
csharp_strings.htm
Advertisements