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 !!
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
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
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