我有一个字符串,其中包含一个移动电话号码,如" 0887543232"。我想用国家代码替换0。我有国家代码变量。下面是我的代码,我试图替换0。
string mobile="0887543232"
string countryCode= "+1"
int len = mobile.Length;
string mob = mobile.Substring(0, 1);
if (mob == "0")
{
mobile = mobile.Replace(mob,countrycode);
}
答案 0 :(得分:2)
您不想替换零,您想要添加国家代码,您可以使用:
if (mobile.StartsWith("0"))
{
mobile = countryCode + mobile.TrimStart('0');
}
答案 1 :(得分:0)
怎么样?
string mobile="0887543232";
string countryCode= "+1";
if(!mobile.StartsWith(countryCode)){
mobile = (countryCode + mobile.TrimStart('0'));
}
如果国家/地区代码尚未包含国家代码,则只会添加国家/地区代码。