Get last 4 digits of a string

Hello,
I have a string variable that contains user phonenumbers. However I am only able to use the last 4 digits of the phonenumber for this process. How can I make such a sequence and what would I use.

Input: 023456789
Output: 6789

Use this below regex ,
StrVar() = System.Text.RegularExpressions.Regex.Matches(“023456789”,“[0-9]{4}$”)(0).ToString

2 Likes

@dvn If you have a String which has less than 4 numbers as well, you can try this :
str.Substring(Math.Max(0, str.Length - 4))

If the String has length less than 3, it will return the String itself.

2 Likes

Hi @dvn,

You can use the below solution.

strVar.Substring(strVar.length-4,4)

Regards
Varsha BS

2 Likes