这将是我的字符串
帕尼帕特,巴特那,
结果应为 Panipat,Patna
下,帕尼帕特,巴特那,
结果应为 Panipat,Patna
帕尼帕特,
结果应为 Panipat
下,帕尼帕特,,
结果应为 Panipat
我该怎么办?需要帮助!!
答案 0 :(得分:3)
看起来你想要Trim
函数
Dim result = input.Trim(New Char() { ","c })
此函数将从字符串值
的开头和结尾删除所有出现的指定字符使用示例
Dim str As String = "hello,"
Dim res = str.Trim(New Char() {","c})
Console.WriteLine(res) 'Prints: hello
答案 1 :(得分:0)
您可以从here
获取更多信息Function RemoveCharacter(ByVal stringToCleanUp, ByVal characterToRemove)
Return stringToCleanUp.Replace(characterToRemove, "")
End Function
试试这个:
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
答案 2 :(得分:0)
实现目标的最佳方法是使用正则表达式。下面的例子一次可以用于两个术语。
例如替换,Panipat,Patna,与Panipat,PatnaPublic 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