Grab random sequence of words from a string?

Zach Johnson

I am searching for the most efficient way to grab a set amount of words (in order) from a string.

So let's say I have a paragraph of text:

"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

I want to be able to grab a variable amount of words at random positions in the paragraph. So if 5 words were wanted an example of some outputs could be:

  • "release of Letraset sheets containing"
  • "Lorem Ipsum is simply dummy"
  • "only five centuries, but also"

What would be the best way of going about doing this?

Cyral

Split the data up by spaces to get a list of words, then find a random place to select the words from (at least 5 words from the end), and then join the words back together.

private static readonly Random random = new Random();
public static void Main(string[] args)
{
    var data =
        "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    Console.WriteLine(GetRandomWords(data, 5));
    Console.ReadLine();
}

private static string GetRandomWords(string data, int x)
{
    // Split data into words.
    var words = data.Split(' ');
    // Find a random place to start, at least x words back.
    var start = random.Next(0, words.Length - x);
    // Select the words.
    var selectedWords = words.Skip(start).Take(x);
    return string.Join(" ", selectedWords);
}

Example output:

the 1960s with the release
PageMaker including versions of Lorem
since the 1500s, when an
leap into electronic typesetting, remaining
typesetting, remaining essentially unchanged. It

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find words from a sequence of random letters?

From Dev

print random string from string with multiply words

From Dev

How can I grab a group of words from a string?

From Dev

how to generate random text from string of words

From Dev

Replace whole words in a string with a random word from an array of grouped words

From Dev

Pick random words from a string array and output them into a TextView

From Dev

Grab Random record from SQL Database and display it

From Dev

Replacing a random string sequence with underscore

From Dev

Grab Amount from Specific String

From Dev

grab just the cents from string

From Dev

grab just the cents from string

From Dev

Replace sequence of words in string with string python

From Dev

Selecting random words from table

From Dev

Stochastic version of seq for generating sequence of random numbers/words?

From Dev

Extract words from string

From Dev

Extract "words" from a string

From Dev

Extract words from string

From Dev

Deleted words from a string

From Dev

Words from string to list

From Dev

c++ Do-While, Random Words From Array For String With No Repitition

From Dev

Substituting percentage of words at random indexes in String array

From Dev

Best way to grab a specific position from a string

From Dev

grab value from string in reverse order

From Dev

regex to grab from char/string to another

From Dev

How to grab text from a messy string in java?

From Dev

Best way to grab a specific position from a string

From Dev

Regex to grab the data from an address string

From Dev

Script that will grab random images from various tags on tumblr

From Dev

How to generate a random sequence of random string characters in Python

Related Related

HotTag

Archive