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