我打开whatsapp url的文本和数字。
问题是我有两种号码
+923323789222 & 03323789222Whatsapp不是从0开始打开数字,所以我需要做的是,如果数字有0替换为+92
var url ='whatsapp://send?phone=+923323789222&text=Apna ${widget.data['selererName']} ko ${Ggive.toString()} Rupees dene hein';在电话中,当我与+92擦肩而过时,它可以正常工作,所以我的问题是,如果我的号码从0开始,用+92替换,我该如何替换?
发布于 2021-05-24 14:50:52
您只需检查您的数字中是否有0,只需用+92替换,如果它不是以0开始,则保持不变。
String num = yournumber.toString();
if(num[0] == "0"){
print('have zero');
String numb2 = num.substring(0, 0) + "+92" + num.substring(1);
print(numb2);
num = numb2;
}
var url ='whatsapp://send?phone=${num}&text=Apna ${widget.data['selererName']} ko ${Ggive.toString()} Rupees dene hein';
print(url);发布于 2021-05-24 15:00:02
只需使用下面的代码替换特定的字符串。
var url ='whatsapp://send?phone=+923323789222&text=Apna ${widget.data['selererName']} ko ${Ggive.toString()} Rupees dene hein';
url.replaceAll('phone=0', 'phone=+92');您还可以使用regex替换字符串。
发布于 2021-05-24 14:57:20
试试这个:
String formatPhoneNumber(String phoneNumber) {
if (phoneNumber.length < 1) return '';
// if the phone number doesn't start with 0,
// it is already formatted and we return in the
// way it is.
if (phoneNumber[0] != '0') return phoneNumber;
// if it starts with 0 then we replace the 0 with +92
// and return the new value.
return phoneNumber.replaceFirst(RegExp('0'), '+92');
}用法:
print(formatPhoneNumber('+923323789222'));
// OUTPUT: +923323789222
print(formatPhoneNumber('03323789222'));
// OUTPUT: +923323789222https://stackoverflow.com/questions/67674018
复制相似问题