How to remove specific characters from a string in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function removeSpecificCharactersFromString(ByVal str As String, ByVal charsToRemove As Char()) As String
        For Each ch As Char In charsToRemove
            str = str.Replace(ch.ToString(), "")
        Next

        Return str
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim phone As String = "(555) 555-5555"
        Dim charsToRemove As Char() = New Char() {"("c, ")"c, "-"c}
        
        phone = removeSpecificCharactersFromString(phone, charsToRemove)
        
        Console.WriteLine(phone)
    End Sub
End Class



' run:
'
' 555 5555555
'

 



answered Mar 17, 2024 by avibootz
...