从字符串变量中丢失最后3个字符

时间:2010-03-25 09:54:19

标签: vb.net string trim

如果我的字符串(结果)中的最后4个字符是“AND”或最后三个字符是“OR”我想从字符串中删除它们。到目前为止,我一直在尝试使用result.trimend和其他一些方法,但我不确定如何让它工作。

由于

2 个答案:

答案 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