我的问题是 如何通过代码嵌入此模拟?
如果Label39.text =“a”,则将删除label40.text“a”的内容,字母列表将按字母顺序排列。
我希望我的label39.text随时更改其值“RANDOMLY”时也会发生这种情况
例如,如果label39.text =“a,b,c,d,x,z” label40.text =“e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y”
到目前为止这是我的代码
Dim patterns As String
patterns = Label39.Text
Dim tobefollow As String
tobefollow = Label40.Text
Dim matches As MatchCollection = Regex.Matches(patterns, tobefollow)
If Regex.IsMatch(patterns, tobefollow) Then
'this where i will put my code to make my example
End If
答案 0 :(得分:1)
首先,请注意您错误地填充了patterns
和tobefollow
变量(您在另一个问题中正确地执行了此操作);它应该是:
patterns = Label40.Text
tobefollow = Label39.Text
另请注意,您可以轻松完成所需内容,而无需依赖Regex
;例如通过:
If (Label40.Text.ToLower().Contains(Label39.Text.ToLower())) Then
'this where i will put my code to make my example
End If
关于这次你想要什么,你可以依靠.Replace
:.Replace("text to be deleted", "")
将删除这封信;但你还要考虑逗号。要置于条件内的代码:
Dim origString As String = Label40.Text
Label40.Text = Label40.Text.ToLower().Replace(Label39.Text.ToLower() & ",", "")
If (origString = Label40.Text) Then
'It means that it does not have any comma, that is, refers to the last letter
Label40.Text = Label40.Text.ToLower().Replace("," & Label39.Text.ToLower(), "")
End If
答案 1 :(得分:0)
另一个答案是使用String.Split
和String.Join
方法将字符串细分为单个字符,然后将其从列表中删除并将它们重新连接在一起。这是一个功能:
Private Function RemoveLetters(str1 As String, str2 As String) As String
Dim sep() As String = {","}
Dim list1 As List(Of String) = str1.Split(sep, StringSplitOptions.None).ToList
Dim list2 As List(Of String) = str2.Split(sep, StringSplitOptions.None).ToList
For Each s As String In list2
list1.Remove(s)
Next
Return String.Join(",", list1)
End Function
你会像这样使用它:
Label40.Text = RemoveLetters(Label40.Text, Label39.Text)