丢失字符串变量的最后 3 个字符

发布于 2024-08-27 05:55:36 字数 116 浏览 7 评论 0原文

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2024-09-03 05:55:36

您还可以使用正则表达式:

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

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

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, "")
み格子的夏天 2024-09-03 05:55:36
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文