Trim last 4 characters from string?

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

How can I trim MyString to be MyStr?

Thanks, google failed again :(

4条回答
  •  醉梦人生
    2021-01-04 10:59

    Rob's answer is mostly correct but the SubString solution will fail whenever the string has less than 4 characters in it. If the length goes past the end of the string an exception will be thrown. The following fixes that issue

    Public Function TrimRight4Characters(ByVal str As String) As String
      If 4 > str.Length Then
        return str.SubString(4, str.Length-4)
      Else
        return str
      End if
    End Function
    

提交回复
热议问题