0

I am given a contact number by a customer. The client that this number is given to requires the first number to be omitted from the result. I have used Regex to do this, but I'm curious if there is a more optimal way to do this.

var mobileNumber = "07123123123";
var homeNumber = "01511231231";

var pattern = "(.{10})$";

var omittedMobile = Regex.Split(mobileNumber, pattern)[1];
var omittedHome = Regex.Split(homeNumber, pattern)[1];

var mobileNumber = "07123123123";
var homeNumber = "01511231231";

I receive: 07123123123 - I provide 7123123123

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Howlan
  • 11
  • 2
  • Not very clear what is your objective but a simple _mobileNumber.Substring(1)_ should be enough (and it will work even if you have only 10 or less digits in the string) – Steve Jul 28 '19 at 12:30
  • 1
    It might be worth to sanity-check your requirements: The *real* requirement (as opposed to "the requirement as imagined by your customer") is probably *not* to blindly remove the first digit, but to "start the number with the area code", e.g. `01234...` should become `1234...`, `+441234...` should become `1234...` and `1234` should throw an error. – Heinzi Jul 28 '19 at 13:28
  • The task here looks to be just "get last X chars from string". It has [already been solved](https://stackoverflow.com/questions/6413572/how-do-i-get-the-last-four-characters-from-a-string-in-c). – Wiktor Stribiżew Jul 28 '19 at 15:20

2 Answers2

2

Using string function Substring(int startIndex),

var mobileNumber = "07123123123";
Console.WriteLine(mobileNumber.Substring(1));
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
2

Why not treat it as a simple string and remove the first character?

mobileNumber.Substring(1);

//or

mobileNumber.Remove(0, 1);
Guy
  • 46,488
  • 10
  • 44
  • 88