2010-03-25 3 views
0

의 마지막 3자를 내 문자열 (결과)의 마지막 4 개 문자가 "AND"이거나 마지막 3 개의 문자가 "OR"인 경우 문자열에서 제거하고 싶습니다. 지금까지 나는 result.trimend와 몇 가지 다른 방법을 시도했지만 어떻게 작동하는지 확신 할 수 없습니다.문자열 변수

감사

답변

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 
+0

이 좋은 해결책처럼 보인다을 .. –

+0

제대로 공백을 처리 알고 있어야합니다. 샘플에서 아무것도 제거되지 않습니다. 또한 대소 문자를 구분하지 않고 "OR"대신 "OR"을 확인하는 것이 좋습니다. –

+0

아 **** TrimEnd()를 잊어 버렸습니다! – Snake

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, "")