37

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

4

10 回答 10

62
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("<p>");
        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("</p>");
    }

    return result.ToString();
}
于 2010-11-26T15:33:55.770 回答
29

我为 Ruby Faker gem 编写了一个 C# 端口,可用于轻松生成虚假数据:姓名、地址、电话号码和 lorem ipsum 文本。

它以 NuGet 包 ( Install-Package Faker.Net) 的形式在 Github 上提供,我还写了一篇介绍它的一些功能的文章,并附有示例代码。

于 2012-07-08T20:56:40.010 回答
28

像这样:

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));
于 2010-11-26T15:24:08.580 回答
9

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

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

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

var someComments = new NLipsum.Core.Paragraph();
于 2013-09-18T16:40:38.300 回答
3

为什么不使用 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("<div id=\"lipsum\">")); 
   HTML = HTML.Remove(HTML.IndexOf("</div>"));
   HTML = HTML
        .Replace("<div id=\"lipsum\">", "")
        .Replace("</div>", "")
        .Replace("<p>", "")
        .Replace("</p>", "");

   reader.Close();
   dataStream.Close();
   response.Close();
   return HTML; 
}
于 2012-10-28T08:29:25.430 回答
2

您好
,您可以使用 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 项目站点

于 2013-12-30T10:43:42.430 回答
2

使用 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();
    }
于 2015-05-20T08:26:12.177 回答
2

对 Greg + Tomino 的上述出色方法进行了细微修改,以将每个句子的第一个单词大写。我还删除了尾随的换行符并删除了一些“+ 1”,这些“+ 1”给了一个太多。非常方便测试用户界面的自动换行功能!感谢富野和格雷格。

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();
}
于 2015-09-04T19:19:59.047 回答
0

找到这个 Lorem Ipsum 生成器: http: //www.gutgames.com/post/Lorem-Ipsum-Generator-in-C.aspx

于 2012-08-13T20:49:32.073 回答
0

NuGet 中有一个名为NetFx Ipsum Generator

你可以安装它

Install-Package netfx-IpsumGenerator

虽然它非常小,但我目前正在寻找一个更好的,或者一种贡献的方式。

于 2011-11-12T00:57:52.083 回答