Home   Cover Cover Cover Cover
 

Buchstabenextraktion

A01.cs
/* Extraction of letters from a string
   This example implements a method Where, which extracts from a string
   all letters that match a certain criterion. The result is returned as
   a string again.
------------------------------------------------------------*/
using System;
using System.Text;

public class A1 {
  
  public static string Where(string s, Func<char, bool> matches) {
    StringBuilder b = new StringBuilder();
    foreach (char ch in s) {
      if (matches(ch)) b.Append(ch);
    }
    return b.ToString();
  }

  public static void Main() {
    string[] fruits = {"Apple", "Orange", "Banana"};
    //-- extracts all vovels from a string
    foreach (string s in fruits) {
      Console.WriteLine(s + " => " + Where(s, ch => "aeiouAEIOU".IndexOf(ch) >= 0));
    }
    //-- extracts all upper case letters from a string
    foreach (string s in fruits) {
      Console.WriteLine(s + " => " + Where(s, ch => char.IsUpper(ch)));
    }
  }
}