在c#中有没有lorem ipsum生成器?

我正在寻找ac#generator,它可以生成随机单词,句子,由多个单词/段落给出的段落以及某些语法,如地址,数字,邮政编码/邮政编码,国家,电话号码,电子邮件地址。

static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numParagraphs) { var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"}; var rand = new Random(); int numSentences = rand.Next(maxSentences - minSentences) + minSentences + 1; int numWords = rand.Next(maxWords - minWords) + minWords + 1; StringBuilder result = new StringBuilder(); for(int p = 0; p < numParagraphs; p++) { result.Append("

"); for(int s = 0; s < numSentences; s++) { for(int w = 0; w < numWords; w++) { if (w > 0) { result.Append(" "); } result.Append(words[rand.Next(words.Length)]); } result.Append(". "); } result.Append("

"); } return result.ToString(); }

我写了一个Ruby Faker gem的C#端口,可以用来轻松生成假数据:名称,地址,电话号码和lorem ipsum文本。

它可以作为一个NuGet包( Install-Package Faker.Net )在Github上提供源代码,我还写了一篇文章,介绍了它的一些function,并附带了示例代码。

  • Faker源代码 (在github上)
  • Faker.Net NuGet包
  • 使用C#填充测试数据以进行实际的单元和集成测试

像这样:

 const string LoremIpsum = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; 

重复一遍:

 String.Join(Environment.NewLine, Array.ConvertAll(new int[count], i => LoremIpsum)); 

或者,在.Net 4.0中:

 String.Join(Environment.NewLine, Enumerable.Repeat(LoremIpsum, count)); 

在Nuget上实际上有一个包装可以为你做这件事。

http://www.nuget.org/packages/NLipsum/

例如,您可以通过执行以下操作生成一段文本:

 var someComments = new NLipsum.Core.Paragraph(); 

为什么不使用Lorem Ipsum Online生成器?

我写了这段代码,从HTML页面中提取lorem ispum字符串:

 string LoremIpsum() { string HTML = null; WebRequest request = WebRequest.Create("http://lipsum.com/feed/html"); request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); HTML = reader.ReadToEnd(); //se citeste codul HTMl //searching for Lorem Ipsum HTML = HTML.Remove(0, HTML.IndexOf("
")); HTML = HTML.Remove(HTML.IndexOf("
")); HTML = HTML .Replace("
", "") .Replace("
", "") .Replace("

", "") .Replace("

", ""); reader.Close(); dataStream.Close(); response.Close(); return HTML; }

你好
您可以使用MMLib.RapidPrototyping nuget包中的WordGenerator或LoremIpsumGenerator。

 using MMLib.RapidPrototyping.Generators; public void Example() { WordGenerator generator = new WordGenerator(); var randomWord = generator.Next(); Console.WriteLine(randomWord); LoremIpsumGenerator loremIpsumGenerator = new LoremIpsumGenerator(); var text = loremIpsumGenerator.Next(3,3); Console.WriteLine(text); } 

Nuget网站
Codeplex项目站点

使用StringBuilder且没有HTML标签的版本(使用新行而不是段落标记):

  private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines) { var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"}; var rand = new Random(); int numSentences = rand.Next(maxSentences - minSentences) + minSentences + 1; int numWords = rand.Next(maxWords - minWords) + minWords + 1; var sb = new StringBuilder(); for (int p = 0; p < numLines; p++) { for (int s = 0; s < numSentences; s++) { for (int w = 0; w < numWords; w++) { if (w > 0) { sb.Append(" "); } sb.Append(words[rand.Next(words.Length)]); } sb.Append(". "); } sb.AppendLine(); } return sb.ToString(); } 

对Greg + Tomino上面出色的方法进行了一些修改,以便将每个句子的第一个单词大写。 我还删除了尾随的换行符,并删除了一些“+ 1”,它们给了太多。 非常方便用于测试用户界面的自动换行function! 感谢Tomino&Greg。

 private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines) { var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"}; var rand = new Random(); int numSentences = rand.Next(maxSentences - minSentences) + minSentences; int numWords = rand.Next(maxWords - minWords) + minWords; var sb = new StringBuilder(); for (int p = 0; p < numLines; p++) { for (int s = 0; s < numSentences; s++) { for( int w = 0; w < numWords; w++ ) { if( w > 0 ) { sb.Append( " " ); } string word = words[ rand.Next( words.Length ) ]; if( w == 0 ) { word = word.Substring( 0, 1 ).Trim().ToUpper() + word.Substring( 1 ); } sb.Append( word ); } sb.Append(". "); } if ( p < numLines-1 ) sb.AppendLine(); } return sb.ToString(); } 

NuGet中有一个名为NetFx Ipsum Generator 。

你可以安装它

 Install-Package netfx-IpsumGenerator 

尽管如此,我目前正在寻找一个更好的,或者一种贡献的方式。

发现这个Lorem Ipsum发电机: http : //www.gutgames.com/post/Lorem-Ipsum-Generator-in-C.aspx