检查两个字符串是否是 VB 中的字谜
Check if two strings are anagrams in VB
我正在 VB 中制作一个小的字谜解决游戏,我想验证给定的单词是否符合给定的字母。例如,字符串 eliter
不应与 letter
匹配,因为 t
在上述字符串中只出现一次。我已经有一个检查来确定提交的内容是否是一个单词,但理想情况下,检查以确保它符合给定的字母将围绕该语句进行检查。
Public Class Form1
Public Function GenerateRandomString(ByRef iLength As Integer) As String
Dim rdm As New Random()
Dim allowChrs() As Char = "ABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
Dim sResult As String = ""
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
countdown.Enabled = True
lbl_countdown.Text = "10"
lbl_anagram.Text = GenerateRandomString(9)
End Sub
Private Sub countdown_Tick(sender As Object, e As EventArgs) Handles countdown.Tick
lbl_countdown.Text -= 1
If lbl_countdown.Text < 1 Then
countdown.Enabled = False
MsgBox("End of game")
Dim wordList As HashSet(Of String) = New HashSet(Of String)(File.ReadAllLines("words_alpha.txt"))
If Not wordList.Contains(TextBox1.Text.ToString()) Then
MsgBox("Not found")
Else
MsgBox("Found")
End If
End If
End Sub
End Class
请注意,这需要在 VB 中并且最好不需要额外的库等
另请注意,您不必使用所有字母,但不能使用超过可用的字母!
提前致谢!
您只需要按字母顺序对字符串进行排序并比较排序后的字符串。两个字谜在字符排序后会有相同的字符串。
Sub Main()
Dim s As String = "betjwepfw"
Console.WriteLine(s)
Console.WriteLine(New String(s.OrderBy(Function(c) c).ToArray()))
Console.ReadLine()
End Sub
我正在 VB 中制作一个小的字谜解决游戏,我想验证给定的单词是否符合给定的字母。例如,字符串 eliter
不应与 letter
匹配,因为 t
在上述字符串中只出现一次。我已经有一个检查来确定提交的内容是否是一个单词,但理想情况下,检查以确保它符合给定的字母将围绕该语句进行检查。
Public Class Form1
Public Function GenerateRandomString(ByRef iLength As Integer) As String
Dim rdm As New Random()
Dim allowChrs() As Char = "ABCDEFGHIJKLOMNOPQRSTUVWXYZ".ToCharArray()
Dim sResult As String = ""
For i As Integer = 0 To iLength - 1
sResult += allowChrs(rdm.Next(0, allowChrs.Length))
Next
Return sResult
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
countdown.Enabled = True
lbl_countdown.Text = "10"
lbl_anagram.Text = GenerateRandomString(9)
End Sub
Private Sub countdown_Tick(sender As Object, e As EventArgs) Handles countdown.Tick
lbl_countdown.Text -= 1
If lbl_countdown.Text < 1 Then
countdown.Enabled = False
MsgBox("End of game")
Dim wordList As HashSet(Of String) = New HashSet(Of String)(File.ReadAllLines("words_alpha.txt"))
If Not wordList.Contains(TextBox1.Text.ToString()) Then
MsgBox("Not found")
Else
MsgBox("Found")
End If
End If
End Sub
End Class
请注意,这需要在 VB 中并且最好不需要额外的库等
另请注意,您不必使用所有字母,但不能使用超过可用的字母!
提前致谢!
您只需要按字母顺序对字符串进行排序并比较排序后的字符串。两个字谜在字符排序后会有相同的字符串。
Sub Main()
Dim s As String = "betjwepfw"
Console.WriteLine(s)
Console.WriteLine(New String(s.OrderBy(Function(c) c).ToArray()))
Console.ReadLine()
End Sub