我正在尋找一個c#生成器,它可以生成隨機單詞,句子,段落給出的一些單詞/段落和某些語法,如地址,數字,郵政編碼/郵政編碼,國家,電話號碼,電子郵件地址。在c#中有沒有lorem ipsum生成器?
回答
像這樣:
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-26 15:25:29
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();
}
這將是使用StringBuilder而不是衆多字符串連接的好時機。 – JimmyBoh 2015-04-20 18:49:16
@JimmyBoh更新爲用戶StringBuilder。帶着大量文字來永恆。 – 2015-11-03 16:06:09
有一個在的NuGet叫NetFx Ipsum Generator。
您可以
Install-Package netfx-IpsumGenerator
這是相當小,雖然,我目前正在尋找一個更好的,還是有辦法有助於安裝。
我寫了Ruby Faker gem的C#端口,可以用來輕鬆生成假數據:名稱,地址,電話號碼和lorem ipsum文本。
它作爲一個NuGet軟件包(Install-Package Faker.Net
)在Github上提供了源代碼,我還寫了一篇文章介紹它的一些功能,並帶有示例代碼。
發現這個Lorem存有發生器:http://www.gutgames.com/post/Lorem-Ipsum-Generator-in-C.aspx
爲什麼不使用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;
}
實際上,有上的NuGet包出來做這個確切的事情你。
http://www.nuget.org/packages/NLipsum/
例如,你可以只是這樣做產生了一段文字:
var someComments = new NLipsum.Core.Paragraph();
你好
可以使用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
版本:
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();
}
稍作修改格雷格+富野的好方法以上將每個句子的第一個單詞大寫。我也刪除了後面的換行符,並刪除了一些給出太多的「+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();
}
- 1. 離線lorem ipsum發電機
- 2. 爲什麼我有填充左上方第一個lorem ipsum?
- 3. 爲什麼設計師使用「Lorem Ipsum Dolor」填充空間?
- 4. 如何在Sublime Text 3中自定義默認的「Lorem ipsum」片段?
- 5. 有沒有Cocoa的表單生成器?
- 6. C++從源碼生成器生成文件 - 有沒有這樣的?
- 7. 有沒有去產生在C#中唯一的機器哈希
- 8. 在jsf中沒有會話生成
- 9. _id沒有在Elasticsearch中自動生成
- 10. R.java沒有在Android中生成R.id
- 11. XCode8 TextureAtlas編譯器沒有生成
- 12. JHipster生成器:addMavenDependency沒有定義
- 13. 接口生成器沒有響應?
- 14. 「android.app.Notification $生成器」類沒有發現
- 15. 生成器函數沒有被執行
- 16. 沒有界面生成器的UIPopoverController?
- 17. 沒有生成爲opencv_contrib生成文件
- 18. 在C中生成私有RSA密鑰#
- 19. Symfony管理生成器生成模塊沒有樣式
- 20. GWT請求生成器沒有生產工作,但在發展
- 21. 在Javascript中沒有使用隨機數字生成器?
- 22. angular-fullstack沒有出現在yeoman生成器列表中?
- 23. R.java文件沒有生成?
- 24. ASPXAUTH cookie沒有生成
- 25. Eclipse沒有生成R.java
- 26. jekyll沒有生成index.html
- 27. Eclipse ADT沒有生成MainActivity
- 28. RealmProxy文件沒有生成
- 29. Ember-index沒有生成index.jsp
- 30. T4MVC沒有生成動作
您是否試過Google? http://www.google.nl/search?q=lorem+ipsum+c%23 - 首次擊中看起來很有用。 – Marijn 2010-11-26 15:27:14
我GOOGLE了它,第一個結果把我帶到這裏:)。 – RayLoveless 2013-03-04 05:27:56