Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last 5 characters in a string

Tags:

vb.net

I want to get the last 5 digits/characters from a string. For example, from "I will be going to school in 2011!", I would like to get "2011!".

Any ideas? I know Visual Basic has Right(string, 5); this didn't work for me and gave me an error.

like image 236
NULL Avatar asked May 11 '10 18:05

NULL


People also ask

How do I get the last 5 characters of a string in Python?

The last character of a string has index position -1. So, to get the last character from a string, pass -1 in the square brackets i.e. It returned a copy of the last character in the string. You can use it to check its content or print it etc.

How do I get the last 3 characters of a string?

Getting the last 3 characters To access the last 3 characters of a string, we can use the built-in Substring() method in C#. In the above example, we have passed s. Length-3 as an argument to the Substring() method.


7 Answers

str.Substring(str.Length - 5)
like image 137
dcp Avatar answered Nov 10 '22 09:11

dcp


Error check:

result = str.Substring(Math.Max(0, str.Length - 5))
like image 37
Glennular Avatar answered Nov 10 '22 08:11

Glennular


Checks for errors:

Dim result As String = str
If str.Length > 5 Then
    result = str.Substring(str.Length - 5)
End If
like image 23
Nick Gotch Avatar answered Nov 10 '22 09:11

Nick Gotch


in VB 2008 (VB 9.0) and later, prefix Right() as Microsoft.VisualBasic.Right(string, number of characters)

Dim str as String = "Hello World"

Msgbox(Microsoft.VisualBasic.Right(str,5))

'World"

Same goes for Left() too.

like image 45
Umesh Vora Avatar answered Nov 10 '22 08:11

Umesh Vora


Old thread, but just only to say: to use the classic Left(), Right(), Mid() right now you don't need to write the full path (Microsoft.VisualBasic.Strings). You can use fast and easily like this:

Strings.Right(yourString, 5)
like image 31
Toni Avatar answered Nov 10 '22 07:11

Toni


The accepted answer of this post will cause error in the case when the string length is lest than 5. So i have a better solution. We can use this simple code :

If(str.Length <= 5, str, str.Substring(str.Length - 5))

You can test it with variable length string.

    Dim str, result As String
    str = "11!"
    result = If(str.Length <= 5, str, str.Substring(str.Length - 5))
    MessageBox.Show(result)
    str = "I will be going to school in 2011!"
    result = If(str.Length <= 5, str, str.Substring(str.Length - 5))
    MessageBox.Show(result)

Another simple but efficient solution i found :

str.Substring(str.Length - Math.Min(5, str.Length))

like image 31
Md. Suman Kabir Avatar answered Nov 10 '22 07:11

Md. Suman Kabir


I opened this thread looking for a quick solution to a simple question, but I found that the answers here were either not helpful or overly complicated. The best way to get the last 5 chars of a string is, in fact, to use the Right() method. Here is a simple example:

Dim sMyString, sLast5 As String

sMyString = "I will be going to school in 2011!"
sLast5 = Right(sMyString, - 5)
MsgBox("sLast5 = " & sLast5)

If you're getting an error then there is probably something wrong with your syntax. Also, with the Right() method you don't need to worry much about going over or under the string length. In my example you could type in 10000 instead of 5 and it would just MsgBox the whole string, or if sMyString was NULL or "", the message box would just pop up with nothing.

like image 37
SendETHToThisAddress Avatar answered Nov 10 '22 07:11

SendETHToThisAddress