簡體   English   中英

我如何打亂或隨機化文本文件中的所有單詞?

[英]how i can shuffle or randomize all words in textfile?

我需要隨機化或打亂文本文件中的所有單詞。

例如在我的文本文件中,我有以下單詞

hello world i have good one
today is good day
it good to see you
all that kind thing world 
boy man women girl 

在洗牌或隨機化后,我想得到這樣的結果

is see kind boy today
world all hello women
man that to you day
girl it i one good
have thing world

我可以在 vb.net 中做什么? 提前致謝


解析模塊 Module1

        Sub Main()
            Dim lines() As String = IO.File.ReadAllLines("C:\Users\Username\Desktop\nice.txt")
            Dim outputData As String = RandomizeWords(lines)

            IO.File.WriteAllText("C:\Users\Username\Desktop\nice123.txt", outputData)
            Console.ReadLine()
        End Sub

        Private Function RandomizeWords(lines() As String) As String

            Dim sb As New StringBuilder()
            Dim newList As New List(Of String)

            For Each line In lines
                Dim orderedWords() As String = line.Split(" "c)
                Dim randomizedWords = From n In orderedWords Order By Guid.NewGuid() Select n

                newList.AddRange(randomizedWords)
            Next

            Dim wordCount As Integer = 0

            For i = 0 To newList.Count - 1
                Dim randomIndex As Integer = New Random().Next(0, newList.Count - 1)

                If wordCount = 4 Then
                    sb.AppendLine(newList(randomIndex))
                    wordCount = 0
                Else
                    sb.Append(newList(randomIndex) & " ")
                End If

                newList.RemoveAt(randomIndex)

                wordCount += 1
            Next

            Return sb.ToString()
        End Function
    End Module
  1. 將每個單獨的單詞提取為字符串並將其添加到字符串數組中。

  2. 洗牌你的字符串數組。

  3. 以新的洗牌順序打印出數組中的單詞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM