tags:

views:

58

answers:

3

if the last 4 chars in my string(result) are " AND" or the last three chars are " OR" I would like to remove these from the string. So far I have am trying result.trimend and a few other methods but am unsure how to get it working.

Thanks

A: 

Substring

Jammin
+2  A: 
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
Snake
Seems like good solution..
Muhammad Akhtar
Be aware of handling the blanks properly. In your sample nothing will be removed. It might also be a good idea to make the check case-insensitive and to check for " OR" instead of "OR".
0xA3
Oh **** I forgot to do TrimEnd()!
Snake
+4  A: 

You could also use regex:

Dim regex As New Regex("\s$|\s?and\s?|\s?or\s?", RegexOptions.IgnoreCase)

Dim mystring As String = "asdfasd AND "
mystring = regex.Replace(mystring, "")
Fabian