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

时间:2010-11-26 15:22:28

标签: c# lorem-ipsum

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

10 个答案:

答案 0 :(得分:52)

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

答案 1 :(得分:26)

像这样:

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

答案 2 :(得分:26)

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

它可以作为NuGet包(Install-Package Faker.Net)在Github上使用,我也写了一篇文章,介绍了它的一些功能,并提供了示例代码。

答案 3 :(得分:5)

在Nuget上实际上有一个包给你做这件事。

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

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

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

答案 4 :(得分:2)

为什么不使用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; 
}

答案 5 :(得分: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 site
Codeplex project site

答案 6 :(得分: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();
    }

答案 7 :(得分:2)

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

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

答案 8 :(得分:0)

NuGet中有一个名为NetFx Ipsum Generator

您可以使用

进行安装
Install-Package netfx-IpsumGenerator

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

答案 9 :(得分:0)