Click here to Skip to main content
14,829,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to auto remove special character that will be displayed in the textbox. the data from the database is like this -->1234-567-89

but i need to display it in the interface like this -->123456789.

Can anyone teach me what to add in my code so i can auto remove the special character when the data are displayed..

How about this:

Me.TextBox1.Text = Me.TextBox1.Text.Replace("-", "")

- Pete
David Knechtges 6-Sep-13 9:08am
That won't work - he has 2 dashes at the beginning of the string.
pdoxtader 6-Sep-13 9:22am
Actually, my answer is just plain correct. It doesn't matter how many dashes there are, or what order they are in, or if they are adjacent. They will be removed.

I just tested it to confirm this, by the way, before I responded to you to make sure I was right.

- Pete

Oh - and the two dashes? That's part of an arrow he was drawing for us, pointing to his example text - not the example of his string.
David Knechtges 6-Sep-13 9:26am
Yes it is right IF the data coming back from the database does not include the "-->" at the beginning of the string. Maybe it is his question that is unclear to me. I thought his database was sending back "-->1234-567-89" and he wanted "-->123456789", which in the solution I posted below is correct.
pdoxtader 6-Sep-13 9:32am
Ok, so if the arrow is part of the string returned from the database, then he will be left with a ">" after using my solution. But my solution is just an example of how to remove specific characters from a string... I would hope he'd realize he can just use it again to remove the ">" that's left behind in that case, ie:

Me.TextBox1.Text = Me.TextBox1.Text.Replace(">", "")

- Pete
Use System.Text.RegularExpressions

See the following code Example :
Imports System.Text.RegularExpressions

Private Function ConvertToCharNumber(ByVal Value As String) As String
            Value = Regex.Replace(Value, "[^\w]", "")
            Return Value
        Catch ex As Exception
            Return Value
        End Try
End Function

How to Use:
Messagebox.Show(ConvertToCharNumber("abcd-@#$1234")) 'Will Return abcd1234

I hope it will help you. :)
Pete's answer is almost correct, but he doesn't account for your 2 dashes at the beginning of the string.

Try this instead:

Me.TextBox1.Text = Me.TextBox1.Substring(3).Replace("-", "")

pdoxtader 6-Sep-13 9:24am
Please see my reply to your comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900