获取字符串中的最后5个字符

NUL*_*ULL 24 vb.net

我想从字符串中获取最后5位数/字符.例如,从中"I will be going to school in 2011!",我想得到"2011!".

有任何想法吗?我知道Visual Basic有Right(string, 5); 这对我没用,给了我一个错误.

dcp*_*dcp 56

str.Substring(str.Length - 5)
Run Code Online (Sandbox Code Playgroud)

  • 只要确保上面的`str.Length - 5`不会产生负值. (18认同)

Gle*_*lar 25

错误检查:

result = str.Substring(Math.Max(0, str.Length - 5))
Run Code Online (Sandbox Code Playgroud)


Nic*_*tch 5

检查错误:

Dim result As String = str
If str.Length > 5 Then
    result = str.Substring(str.Length - 5)
End If
Run Code Online (Sandbox Code Playgroud)