Check if string starts with specific string in C# and VB.Net

Last Reply one month ago By irshad1231

Posted one month ago

Dear All,

I have a mobile number starting with 0 and 966. Like below

0123456789

966123456789

I trying to remove 0 and 966 from the string but want to check the string starting with 0 or 966.

I'm using the below code

string mobileno = "966123456789";//starting with 966
string finalmobileno = Regex.Replace(nationalID, "966", "", RegexOptions.IgnoreCase);
lbl_mobile.Text = finalmobileno.ToString();

string mobileno ="0123456789";//starting with 0
string finalmobileno = iqamano.Substring(1);
lbl_mobile.Text = finalmobileno.ToString();

How can I do it one to check both?

Thanks

You are viewing reply posted by: Saalim one month ago.
Posted one month ago Modified on one month ago

Try this without Regex simple and easy.

string mobileno = "966123456789";
string finalmobileno = "";
if (mobileno.StartsWith("966")) //starting with 966
{
    finalmobileno = mobileno.Substring(mobileno.IndexOf("966") +3);
}
lbl_mobile.Text = finalmobileno.ToString();

string iqamano = "0123456789";
if (iqamano.StartsWith("0")) //starting with 0
{
    finalmobileno = iqamano.Substring(iqamano.IndexOf("0") +1);
}
lbl_mobile.Text = finalmobileno.ToString();