user13355633
user13355633

Reputation:

AppendLine and replacing

I want to get my output from

223-F456

to

223-F456

223-F456#

223-F4560

223-#456

I've done simple code for this but only adding #,0 to the ends of lines works:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sb As New StringBuilder
        For Each line In TextBox1.Lines
            sb.AppendLine(line)
            sb.AppendLine(line & "#")
            sb.AppendLine(line & "0")
            sb.Replace(line.LastIndexOf("-") + 1, "#") -> this doesn't work 
        Next
        TextBox2.Text = sb.ToString

output:

223-F456

223-F456#

223-F4560

The letter is not always "F": I want to replace first letter after "-" not replace "F", also might some serials have "F" letter that not what I want.

Simple products serial in shop but replacing the string after "-" doesn't work and doesn't show, any help would be appreciated.

Upvotes: 2

Views: 264

Answers (2)

Andrew Morton
Andrew Morton

Reputation: 25013

You can use String.Remove and String.Insert like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim sb As New StringBuilder()

    For Each line In TextBox1.Lines
        sb.AppendLine(line)
        sb.AppendLine(line & "#")
        sb.AppendLine(line & "0")
        Dim replaceAt = line.LastIndexOf("-") + 1
        sb.AppendLine(line.Remove(replaceAt, 1).Insert(replaceAt, "#"))
    Next

    TextBox2.Text = sb.ToString()

End Sub

Or if you preferred then you could use the String.Substring function:

    sb.AppendLine(line.Substring(0, replaceAt) & "#" & line.Substring(replaceAt + 1))

It would also be possible to use a regular expression to do the replace, I expect someone else could come up with a better regex, but this works:

    Dim re As New Regex("(.*-)([^-])(.*)")
    For Each line In TextBox1.Lines
        ' ...
        sb.AppendLine(re.Replace(line, "$1#$3"))

Upvotes: 1

ADyson
ADyson

Reputation: 61839

The problem is that

line.LastIndexOf("-") + 1

will return a number (indicating the place in line where it found the last - character, plus one).

But the replace function expects you to tell it what string you want to replace. You want to replace F, not a number. (Your current code would resolve to sb.Replace(4, "#"), which clearly won't find any 4s to replace.)

And if you do sb.Replace it'll replace it for all the versions of the string you've previously added to the stringbuilder. Therefore, it would make more sense to replace it in line, and then append that modified version of line to the stringbuilder.

sb.AppendLine(line.Replace("F", "#"))

Live demo: https://dotnetfiddle.net/5BZAUg


Edit:

If you need to replace any character following the -, not specifically an F, you can do it by finding the character index in line, and then removing the character at that index and replacing it with another one, e.g.

Dim index = line.LastIndexOf("-") + 1
sb.AppendLine(line.Remove(index, 1).Insert(index, "#"))

Demo: https://dotnetfiddle.net/RTbiUG

Upvotes: 1

Related Questions