-1

Can anyone give me an example of spintax snippet for C# / VB.NET programming language. If you don't know what that is (the spintax), well basically it is a way of putting different values of strings and then randomly choosing one. For instance:

{Hello|Hi|Greetings} my name is {Tom|John|Eaven} and I like {turtles|programming|ping pong}.

And it would choose between { } splitting those strings inside of the {} string with delimiter of | so it randomly outputs the final string.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 5
    I'm not the down voter, but - you should really give this a try yourself and provide some code. People here won't write it for you in general if you don't try. This wouldn't be too hard to figure out with 5-10 minutes of googling. :p – John Humphreys Dec 02 '11 at 16:26

1 Answers1

5

Here is a class for C# which handles that snippet:

public class Spinner
{
    private static Random rnd = new Random();
    public static string Spin(string str)
    {
        string regex = @"\{(.*?)\}";
        return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
    }
    public static string WordScrambler(Match match)
    {
        string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
        return items[rnd.Next(items.Length)];
    }
}

And try it:

Console.WriteLine(Spinner.Spin("{Hello|Greetings|Merhaba} World, My name is {Beaver|Michael} Obama"));

Here is the complete article: http://jeremy.infinicastonline.com/2010/11/spintax-class-for-c-net/

Tarik
  • 79,711
  • 83
  • 236
  • 349