更改给定文本框中的句子顺序

时间:2018-06-05 18:58:14

标签: vb.net visual-studio

我虽然关于解决方案,但我不知道怎么做。

例如,我在文本框中的每一行都有一些句子。

Sentence 1
Sentence 2
Sentence 3

我想要输出"混合/ randomed"类似的东西:

Sentence 3
Sentence 1
Sentence 2

有什么想法吗?我编程不好。

1 个答案:

答案 0 :(得分:-1)

你可以使用Linq的OrderBy扩展方法来实现这一目的。

    'Setup
    Dim txtYourTextBox As New TextBox
    txtYourTextBox.Text = "Sentence 1" & ControlChars.NewLine & "Sentence 2" & ControlChars.NewLine & "Sentence 3"

    'Creates random numbers.
    Dim rnd As New System.Random()

    'Get the list of sentences randomized.
    Dim lstRandomizedSentences As List(Of String) = txtYourTextBox.Text.Split(ControlChars.NewLine).OrderBy(Function() rnd.Next).ToList()

    'Clear the textbox before repopulating with randomized sentences.
    txtYourTextBox.Text = String.Empty

    'Loop through all of your randomized sentences to populate the textbox.
    For Each strSentence In lstRandomizedSentences

        '.Trim() so extra line breaks aren't added.
        txtYourTextBox.Text += strSentence.Trim() & ControlChars.NewLine

    Next