如果我有一个字符串如下所示:
10,000kg crane,21
如何去除除了最后一个逗号外的所有逗号
10000kg crane,21
我觉得这是一个正则表达式的问题。
另一种方法,可能比正则表达式解决方案更快:
Dim s As String = "10,000kg crane,21"
Dim result As String = New StringBuilder(s).Replace(",", String.Empty, 0,
s.LastIndexOf(","c)).ToString()
一种非正则表达式的方法:
Dim text = "10,000kg crane,21"
Dim parts = text.Split(","c).Reverse
Dim result = String.Join("", parts.Skip(1).Reverse) & "," & parts.First
一种更丑陋但有效的替代方法:
Dim strResult As String = Replace(Mid(strTarget, 1, strTarget.LastIndexOf(",")), ",", String.Empty) & _
Microsoft.VisualBasic.Right(strTarget, Len(strTarget) - strTarget.LastIndexOf(","))