user918477
user918477

Reputation:

remove special character from string in vb.net

this would be my string

Panipat,Patna,

Result should be Panipat,Patna

,Panipat,Patna,

Result should be Panipat,Patna

Panipat,

Result should be Panipat

,Panipat,,

Result should be Panipat

How can i do it . Need help !!

Upvotes: 1

Views: 9273

Answers (3)

irfanmcsd
irfanmcsd

Reputation: 6561

Best way to achieve your goal is use regular expression. Below example will work for two terms at a time.

e.g replace ,Panipat,Patna,, with Panipat,Patna

Public Function Return_Strip_Word(Text As String) As String
    Dim reg As String = "(?<stripword>(\w+)\b,\b(\w+))"
    Dim VDMatch As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Text, reg)
    If VDMatch.Success Then
         Return VDMatch.Groups("stripword").Value
    Else
        Return Text
    End If
End Function

Upvotes: 0

coder
coder

Reputation: 13250

You can get more info from here

Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
   Return stringToCleanUp.Replace(characterToRemove, "")
End Function

Try with this:

Private Sub Main()
    Dim inputText As String = "This is string ending with comma character,"

    Dim result As String = inputText.Trim(",".ToCharArray())

    Console.WriteLine(result)

End Sub

Upvotes: 0

JaredPar
JaredPar

Reputation: 754615

It looks like you want the Trim function

Dim result = input.Trim(New Char() { ","c })

This function will remove all occurrences of the specified characters from the start and end of the string value

Example usage

Dim str As String = "hello,"
Dim res = str.Trim(New Char() {","c})
Console.WriteLine(res) 'Prints: hello

Upvotes: 3

Related Questions