// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class FindName { public delegate bool InUse(string s); public static string FindFree(string first, string last, InUse test) { string s; s = first; if(!test(s)) return s; s = last; if(!test(s)) return s; s = first[0] + last; if(!test(s)) return s; s = first + last[0]; if(!test(s)) return s; s = first + last; if(!test(s)) return s; s = first[0] + "_" + last; if(!test(s)) return s; s = first + "_" + last[0]; if(!test(s)) return s; s = first + "_" + last; if(!test(s)) return s; int i = 1; for(;;) { i++; s = first + i; if(!test(s)) return s; } } public static Random rng = new Random(); public static bool InDbSimulator(string s) { return (rng.NextDouble() < 0.9); } public static void Main(string[] args) { for(int i = 0; i < 100; i++) { Console.WriteLine(FindFree("neo", "man", InDbSimulator)); } } } }