VB.Net / C# Spintax help?
本文关键字:help Spintax Net VB | 更新日期: 2023-09-27 18:16:24
谁能给我一个例子spintax片段c#/VB。. NET编程语言。如果你不知道那是什么(自旋税)基本上它是一种放不同字符串值然后随机选择一个的方法。例如:
{Hello|Hi|Greetings} my name is {Tom|John|Eaven} and I like {turtles|programming|ping pong}.
它会选择在{}字符串内用分隔符|分割这些字符串,所以它会随机输出最终字符串
下面是一个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/