如果我的字符串(结果)中的最后4个字符是“AND”或最后三个字符是“OR”我想从字符串中删除它们。到目前为止,我一直在尝试使用result.trimend和其他一些方法,但我不确定如何让它工作。
由于
答案 0 :(得分:4)
你也可以使用正则表达式:
Dim regex As New Regex("\s$|\s?and\s?|\s?or\s?", RegexOptions.IgnoreCase)
Dim mystring As String = "asdfasd AND "
mystring = regex.Replace(mystring, "")
答案 1 :(得分:2)
Dim yourString as string = "sfsdsfd OR "
yourString = CleanString(yourString)
Function CleanString(byval theString as String) as String
'clean the last spaces
theString = theString.TrimEnd()
If theString .EndsWith("OR")) Then
theString = theString .Substring(0, yourString.Length - 2)
Else if yourString.EndsWith("AND")) Then
theString = theString .Substring(0, yourString.Length - 3)
End If
Return theString
End Function