2010-11-26 73 views
26

我正在尋找一個c#生成器,它可以生成隨機單詞,句子,段落給出的一些單詞/段落和某些語法,如地址,數字,郵政編碼/郵政編碼,國家,電話號碼,電子郵件地址。在c#中有沒有lorem ipsum生成器?

+5

您是否試過Google? http://www.google.nl/search?q=lorem+ipsum+c%23 - 首次擊中看起來很有用。 – Marijn 2010-11-26 15:27:14

+22

我GOOGLE了它,第一個結果把我帶到這裏:)。 – RayLoveless 2013-03-04 05:27:56

回答

23

像這樣:

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

不錯的答案:)) – 2010-11-26 15:25:29

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

這將是使用StringBuilder而不是衆多字符串連接的好時機。 – JimmyBoh 2015-04-20 18:49:16

+1

@JimmyBoh更新爲用戶StringBuilder。帶着大量文字來永恆。 – 2015-11-03 16:06:09

0

有一個在的NuGet叫NetFx Ipsum Generator

您可以

Install-Package netfx-IpsumGenerator 

這是相當小,雖然,我目前正在尋找一個更好的,還是有辦法有助於安裝。

1

爲什麼不使用Lorem存有在線發生器?

我寫了這個代碼,提取從HTML頁面的排版測試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; 
} 
2

你好
可以使用WordGenerator或LoremIpsumGenerator從MMLib.RapidPrototyping NuGet包。

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
使用StringBuilder的和不包含HTML標籤(使用新的生產線,而不是段落標記)Codeplex project site

2

版本:

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

稍作修改格雷格+富野的好方法以上將每個句子的第一個單詞大寫。我也刪除了後面的換行符,並刪除了一些給出太多的「+1」。測試用戶界面的自動換行功能非常方便!感謝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(); 
}