Trim last 4 characters from string?

前端 未结 4 783
予麋鹿
予麋鹿 2021-01-04 10:23

How can I trim MyString to be MyStr?

Thanks, google failed again :(

4条回答
  •  Happy的楠姐
    2021-01-04 11:04

    This is what I used in my program (VB.NET):

        Public Function TrimStr(str As String, charsToRemove As String)
            If str.EndsWith(charsToRemove) Then
                Return str.Substring(0, str.Length - charsToRemove.Length)
            Else
                Return str
            End If
        End Function
    

    Usage:

    Dim myStr As String = "hello world"
    myStr = TrimStr(myStr, " world")
    

    This is my first answer. Hope it helps someone. Feel free to downvote if you don't like this answer.

提交回复
热议问题