String Markers in .NET

I am writing a .NET application that generates random text based on some input. Therefore, if I have a text like "I love your {lovely|nice|great} dress" , I want to randomly select from lovely/nice/great and use it in the text. Any suggestions in C # or VB.NET are welcome.

+6
source share
4 answers

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) 
+8
source share

This may be a bit of abuse of custom formatting functions available through the ICustomFormatter and IFormatProvider interfaces, but you can do something like this:

 public class ListSelectionFormatter : IFormatProvider, ICustomFormatter { #region IFormatProvider Members public object GetFormat(Type formatType) { if (typeof(ICustomFormatter).IsAssignableFrom(formatType)) return this; else return null; } #endregion #region ICustomFormatter Members public string Format(string format, object arg, IFormatProvider formatProvider) { string[] values = format.Split('|'); if (values == null || values.Length == 0) throw new FormatException("The format is invalid. At least one value must be specified."); if (arg is int) return values[(int)arg]; else if (arg is Random) return values[(arg as Random).Next(values.Length)]; else if (arg is ISelectionPicker) return (arg as ISelectionPicker).Pick(values); else throw new FormatException("The argument is invalid."); } #endregion } public interface ISelectionPicker { string Pick(string[] values); } public class RandomSelectionPicker : ISelectionPicker { Random rng = new Random(); public string Pick(string[] values) { // use whatever logic is desired here to choose the correct value return values[rng.Next(values.Length)]; } } class Stuff { public static void DoStuff() { RandomSelectionPicker picker = new RandomSelectionPicker(); string result = string.Format(new ListSelectionFormatter(), "I am feeling {0:funky|great|lousy}. I should eat {1:a banana|cereal|cardboard}.", picker, picker); } } 
+2
source share
 String.Format("static text {0} more text {1}", randomChoice0, randomChoice1); 
+1
source share

write a simple parser that will get the information in braces, divide it by string.Split , get a random index for this array and re-create the string.

use StringBuilder to build the result due to performance issues with other string operations.

0
source share

All Articles