0

and I was having trouble with a certain function in my program. I'm trying to delete a SINGLE instance of a string of text in a textbox, instead of all instances of the string in the textbox.

Here is my code for the button that I want to execute this function in:

    If codon.Text.Contains(U.Text + "-") Then
        codon.Text = codon.Text.Replace(U.Text + "-", "")
    End If

So, this code replaces a string in codon.text with nothing, in other words, it deletes it.

Here is the picture, for better context.

When I click the button my cursor is hovering over, it deletes every instance of, say, UUG. I wanted to see if there was a way to delete only one instance of UUG.

2 Answers 2

2

String.Replace doesn't offer much in terms of control over which part or how many parts of a string you can replace with another. It replaces all the specified substrings or chars (depending on which overload of the method is used) it encounters.

[TextBox].Text = [TextBox].Text.Replace("[SubString]", "")

Will replace all the "[SubString]" found with an empty string.


You could use Regex.Matches to create a MatchCollection of elements that satisfy a condition given a String input and a value to match.

[result group] = Regex.Matches([InputString], [Match Pattern])

The MatchCollection will return a collection of Match elements found.
Inspect the [MatchCollection].Count value to verify whether the search had any valid results.

Each Match identifies its status (boolean Success), the underlying Value (the Pattern) the Index position of Value inside the [InputString] and its Length.

An example: remove the second occurence of the string "UUG-" inside an input string:

Imports System.Text.RegularExpressions

Dim input As String = "UUG-AAA-UUG-CAC-ACC-GGG-GGG-"
Dim xMatches As MatchCollection = Regex.Matches(input, "UUG" & "-")

Dim output As String = input.Remove(xMatches(1).Index, xMatches(1).Length)

Result of output:

"UUG-AAA-CAC-ACC-GGG-GGG-"

You didn't specify which is the element in the input string that you want to remove. This method lets you choose which one using its Index position and/or the order in which it appears .

Sign up to request clarification or add additional context in comments.

9 Comments

This works, but only up until there is only one string in the textbox. For instance, if I insert three "UUG"s in the textbox, then delete two, it gives me an exception when trying to delete the third one. Any ideas on how to fix this? I've tried clearing the textbox when there's only one item left, but it just threw an exception again.
@w60 Have you checked the [MatchCollection].Count property? If it's = 1, then there's just 1 element in the collection. This element in at Index 0, not 1. -- When you receive an Exception, you have to specify what exception that is. Is it a IndexOutOfRange exception?
Oops, I forgot to change the '1's in the "output" string to '0's. Thanks for your help :D
@Çöđěxěŕ Note written. If you think something could be added to it, let me know.
@Jimi great explanation, this is beneficial to anyone seeing this answer, thanks for the update!
|
2

You not working with string - you are working with collection of codes.
So consider full string as only format you will use for displaying codes you have, but operate on the collection.

Then your code will look much simpler and understandable for others (not as Regex ;))

Dim allCodes As New List(Of String) From { "UUG", "UUG", "CAC", "ACC", "GGG", "GGG" }

' Display all codes
codon.Text = String.Join("-", allCodes) 'output: UUG-UUG-CAC-ACC-GGG-GGG

' Remove the first appearance of given value
allCodes.Remove("UUG") ' allCodes => { "UUG", "CAC", "ACC", "GGG", "GGG" }

Simply one comprehensible line of code.
List.Remove - Removes the first occurrence of a specific object

Other operations with your codes should become simple enough. Based on your logic you can use more then one collection and then have code which will convert those to the string for displaying it to the user.

If you want stick to the current implementation, then you can simply convert string to list of codes, remove first occurence and convert back to string.

Dim codes As String = "UUG-UUG-CAC-ACC-GGG-GGG-"

Dim list = codes.Split("-"c).ToList()
list.Remove("UUG")

codes = String.Join("-", list) 'output: UUG-CAC-ACC-GGG-GGG-

2 Comments

Downvoter - please explain, I will be glad to improve an answer or remove it.
I was wondering the same thing. Adding List.RemoveAt(), this is more likely what I would use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.