Home   Cover Cover Cover Cover
 

Iterieren über Bestandteile eines Domainnamens

A02.cs
//==========================================================
// Iterating over the parts of a domain name
//==========================================================

using System;
using System.Collections.Generic;

//----------------------------------------------------------
// domain class
//----------------------------------------------------------
class Domain {
  string name;
  
  // creates a domain with a certain name
  public Domain(string s) {
    name = s;
  }
  
  // iterator over all parts of the domain name
  public IEnumerator<string> GetEnumerator() {
    string s = String.Copy(name);
    int pos = s.IndexOf('.');
    while (pos > 0) {
      yield return s.Substring(0, pos);
      s = s.Substring(pos + 1);
      pos = s.IndexOf('.');
    }
    if (s.Length > 0) yield return s;
  }
  
}

//----------------------------------------------------------
// test program
//----------------------------------------------------------
class Test {
  
  public static void Main() {
    Domain d = new Domain("www.ssw.uni-linz.ac.at");
    foreach (string s in d)
      Console.WriteLine(s);
  }
}