You can do this with a regex to make a replacement for each {...} . The Regex.Replace function can accept a MatchEvaluator which can execute the logic of choosing a random value from the following options:
Random random = new Random(); string s = "I love your {lovely|nice|great} dress"; s = Regex.Replace(s, @"\{(.*?)\}", match => { string[] options = match.Groups[1].Value.Split('|'); int index = random.Next(options.Length); return options[index]; }); Console.WriteLine(s);
Output Example:
I love your lovely dress
Update: Automatically translated to VB.NET using .NET Reflector :
Dim random As New Random Dim s As String = "I love your {lovely|nice|great} dress" s = Regex.Replace(s, "\{(.*?)\}", Function (ByVal match As Match) Dim options As String() = match.Groups.Item(1).Value.Split(New Char() { "|"c }) Dim index As Integer = random.Next(options.Length) Return options(index) End Function)
Mark byers
source share