user2604154
user2604154

Reputation:

How to remove letters?

i just wanna ask: i have a label40.text with a content of {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z} and i also have have a label39.text that will change its output everytime a certain changes happens.

My question is How can i embed this simulation through a code?

If Label39.text = "a" then the content of label40.text "a" will be remove and the list of alphabets will be remain alphabetically.

I want that also to be happen anytime my label39.text will change its value "RANDOMLY"

Example if label39.text = "a,b,c,d,x,z" then label40.text = "e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y"

this is my code so far

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

Upvotes: 0

Views: 154

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

An alternate answer would to use the String.Split and the String.Join Methods to breakdown your strings into individual characters then remove them from the List and join them back together. Here is a Function that does that:

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

you would use it like this:

Label40.Text = RemoveLetters(Label40.Text, Label39.Text)

Upvotes: 0

user2480047
user2480047

Reputation:

First of all, note that you are populating the patterns and tobefollow variables wrongly (you were doing it right in the other question); it should be:

patterns = Label40.Text
tobefollow = Label39.Text

Also bear in mind that what you want can easily be accomplished without relying on Regex; for example via:

If (Label40.Text.ToLower().Contains(Label39.Text.ToLower())) Then
       'this where i will put my code to make my example 
End If

Regarding what you want this time, you can rely on .Replace: .Replace("text to be deleted", "") will remove this letter; but you have also to account for the commas. Code to be put inside the condition:

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

Upvotes: 1

Related Questions