Skip to content

Instantly share code, notes, and snippets.

@dampee
Created July 3, 2014 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dampee/724a30d8f377ceeb05f4 to your computer and use it in GitHub Desktop.
Save dampee/724a30d8f377ceeb05f4 to your computer and use it in GitHub Desktop.
Lorem ipsum generator in c#
/// <summary>
/// Lorem Ipsum Generator
/// </summary>
/// <remarks>
/// Based on Javascript Version of Marcus Campbell - Version 2.0 (Copyright 2003 - 2005)
/// Open-source code under the GNU GPL: http://www.gnu.org/licenses/gpl.txt
/// </remarks>
/// <example>
/// LoremIpsumBuilder _lib = new LoremIpsumBuilder();
/// string test = _lib.GetLetters();
/// </example>
public class LoremIpsumBuilder
{
static readonly Regex WordSplitter = new Regex(@"\w", RegexOptions.Compiled);
private const string Original = "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.";
readonly List<string> _arrOriginal = new List<string>();
/// <summary>
/// Initializes a new instance of the <see cref="LoremIpsumBuilder"/> class.
/// </summary>
public LoremIpsumBuilder()
{
MatchCollection matches = WordSplitter.Matches(Original);
matches.CopyTo(_arrOriginal.ToArray(), 0);
}
/// <summary>
/// Gets a loremipsum string of the given lenght.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>a lorem ipsum string </returns>
public string GetLetters()
{
return GetLetters(Original.Length);
}
/// <summary>
/// Gets a loremipsum string with length of given lenght of letters.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>A lorem ipsum string with a given length.</returns>
public string GetLetters(int length)
{
var output = new StringBuilder();
if (length == 0)
{ length = Original.Length; }
for (var i = 0; i < length; i++)
output.Append(Original[i]);
return output.ToString();
}
/// <summary>
/// Gets a loremipsum string of given lenght of words.
/// </summary>
/// <param name="length">The length.</param>
/// <returns>a lorem ipsum string</returns>
public string GetWords(int length)
{
var output = new StringBuilder();
//var pattern = @"[\w\!\.\?\;\,\:]+/g";
int arrCounter = 0;
for (int i = 0; i <= length; i++)
{
output.Append(_arrOriginal[arrCounter]);
if (++arrCounter > _arrOriginal.Count)
arrCounter = 0;
}
return output.ToString();
}
/// <summary>
/// Gets a loremipsum string of given lenght of paragraphs.
/// </summary>
/// <returns>a lorem ipsum string</returns>
public string GetParagraphs(int count)
{
var output = new StringBuilder();
for (int i = 0; i <= count; i++)
{
if (i == count)
output.Append(Original);
else
output.Append(Original + "\n\n");
}
return output.ToString();
}
}
@klprageeth
Copy link

There was an issue in the constructor that saying
"System.ArgumentException: 'Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')'"

@sano-apps
Copy link

sano-apps commented Jun 21, 2023

@klprageeth

try this.

public LoremIpsumBuilder()
{
MatchCollection matches = WordSplitter.Matches(Original);
_arrOriginal.AddRange(matches.Cast().Select(match => match.Value));
}

BTW, the function GetWords is not working, and in overall, this code is cryptic and unreadable..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment