P
P
PavelVlas2020-07-23 13:39:52
C++ / C#
PavelVlas, 2020-07-23 13:39:52

How to take random line from textBox MultiLine?

string st1 = textBox2.Text;
Random rnd = new Random();
string random_string = Вот тут надо взять рандомную строчку из st1;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2020-07-23
@PavelVlas

Strings are a set of characters separated by newlines. Therefore:
1. Distill st1 into a string array via Split. Separator - line break;
2. Use Ubound to define the upper bound of the resulting array. (The lower bound is zero);
3. When calling Random, define the lower and upper limits of the range from which the lines will be taken;
4. Call Random, get a random number from the given range.
5. Take the element with this number from the string array. Profit!)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question