Link to home
Start Free TrialLog in
Avatar of newaspdotnet
newaspdotnet

asked on

to extract last digits from a string

How can I extract last four digits from a string "abc1234567"

For eg: I have text box wherein a user can fill his account number.

I need to save last 4 digits of Account number in another string.

How can I do this?

 

Looks like it is a simple problem. Would appreciate any help!

Thanks a lot
SOLUTION
Avatar of exalkonium
exalkonium
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, thats php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mid("abc1234567",len("abc1234567")-4,4)
Avatar of newaspdotnet
newaspdotnet

ASKER

Thanks alot for replying!

I used this and it is giving the exact o/p

origString = "abc1234567"
newString = origString.Substring(origString.Length - 4,4)


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the second argument in substring is not necessary
Yes jaime,

Even if I use newString = origString.Substring(origString.Length - 4) it is giving the same o/p.

So this has confused me. Shall I use the 2nd argument now or not. Both gives the same o/p.

So which one to be used.

Thanks!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Fernando Soto
Hi newaspdotnet;

How about a 1 liner.

using System.Text.RegularExpressions;

            String input = "abc1234567";
            String Last4Digits = Regex.Match(input.Trim(), @"^.*?(\d{4})$").Groups[1].Value;
            MessageBox.Show(Last4Digits);


Fernando