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

Last Reply 24 days ago By irshad1231

Posted 24 days 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: irshad1231 24 days ago.
Posted 24 days ago

I'm doing this as below. It's working.

if (mobileno.StartsWith("966"))
{
    lbl_mobile.Text = mobileno.Remove(0, 3);
}
else
{
    lbl_mobile.Text = mobileno.Remove(0, 1);
}