-1

谁能给我一个 C#/VB.NET 编程语言的 spintax 片段示例。如果你不知道那是什么(spintax),那么基本上它是一种放置不同字符串值然后随机选择一个的方法。例如:

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

它会在 { } 使用分隔符 | 分割 {} 字符串内的这些字符串之间进行选择 所以它随机输出最终的字符串。

4

1 回答 1

5

这是处理该片段的 C# 类:

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)];
    }
}

试试看:

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

这是完整的文章:http: //jeremy.infinicastonline.com/2010/11/spintax-class-for-c-net/

于 2011-12-02T16:25:00.940 回答