Flutter change string if condition - flutter

I am opening whatsapp url with text and number.
Issue is i have 2 types of number
+923323789222 & 03323789222
Whatsapp is not opening number starting from 0 so what i need to do is if number have 0 replace it with +92
var url ='whatsapp://send?phone=+923323789222&text=Apna ${widget.data['selererName']} ko ${Ggive.toString()} Rupees dene hein';
in phone when i am passing with +92 its working fine so my question is how can i replace if my number start with 0 and replace with +92

You can just check if your number have 0 in start just replace with +92 and if its not start with 0 then remain it same.
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);

Just use the below code to replace a particular string.
var url ='whatsapp://send?phone=+923323789222&text=Apna ${widget.data['selererName']} ko ${Ggive.toString()} Rupees dene hein';
url.replaceAll('phone=0', 'phone=+92');
You can also use regex for string replacement.

Try this:
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');
}
Usage:
print(formatPhoneNumber('+923323789222'));
// OUTPUT: +923323789222
print(formatPhoneNumber('03323789222'));
// OUTPUT: +923323789222

Related

How to extract number only from string in flutter?

Lets say that I have a string:
a="23questions";
b="2questions3";
Now I need to parse 23 from both string. How do I extract that number or any number from a string value?
The following code can extract the number:
aStr = a.replaceAll(new RegExp(r'[^0-9]'),''); // '23'
You can parse it into integer using:
aInt = int.parse(aStr);
const text = "23questions";
Step 1: Find matches using regex:
final intInStr = RegExp(r'\d+');
Step 2: Do whatever you want with the result:
void main() {
print(intInStr.allMatches(text).map((m) => m.group(0)));
}

I need to check range of mixed substring

I have a string and say I want to check the last 3 digits of the string for some range.
if the string is like sasdaX01, I need to check the last three digits of the string are between X01-X50.
ANy help would be highly appreciated.
The use spl.string::*; line is mandatory and then you extract your last digits with substring(info, length(info) - 3, 3).
An example:
use spl.string::*;
composite TestComposite {
graph
// ... code generating (stream<rstring info> testStream)
() as PrintTestInfo = Custom(testStream as infoEvents) {
logic
onTuple infoEvents : {
rstring lastDigits = substring(info, length(info) - 3, 3);
boolean matched = (lastDigits >= "X01" && lastDigits <= "X50");
println(info + " matched > " + (rstring)matched) ;
} // onTuple infoEvents
} // PrintTestInfo
} // TestComposite

How can I get an alert if a specific url substring is present and nothing if null

function getCode() {
if (window.location.href.indexOf("?discount=")) {
var url = (document.URL);
var id = url.substring(url.lastIndexOf('=') + 1);
window.alert(id);
return true;
} else {
return false;
}
}
Purpose: When people go to our "Service Request" page using a QR code that has a substring of ?discount=1234. I have been testing by creating an alert box with the discount code showing. Eventually I want to be able to populate that "1234" automatically into a "Discount Code:" text field on page load.
The above is a mixture of a few suggestions when I researched it.
Result: Going to example.com/serviceRequest.html?discount=1234 gives me the appropriate alert "1234", as I want... Going to example.com/serviceRequest.html gives me the alert http://example.com/serviceRequest.html, but I don't want anything to happen if "?discount=" is null.
Any suggestions?
indexOf returns -1 if the search pattern doesn't exist. In JavaScript, anything not a 0 or false or undefined is considered true.
So your line of:
if(window.location.href.indexOf("?discount=")) {
Would better search as:
if(window.location.href.indexOf("?discount=") > -1) {
Try changing your if-statement to:
if(window.location.href.indexOf("?discount=") != -1)
Look up the documentation for ".indexOf". It returns -1 for not found and >= 0 if it is found.
...indexOf("?discount=") >= 0
substring and indexOf return -1 if the text is not found, so you can test for this. E.g.
function getCode() {
if(window.location.href.indexOf("?discount=") != -1) {
var url = (document.URL);
var id = url.substring(url.lastIndexOf('=') + 1);
window.alert(id);
return true;
}
else {
return false;
}
}
You just need to test the indexOf value:
function getCode() {
if (window.location.href.indexOf("?discount=") !== -1) {
var url = (document.URL);
var id = url.substring(url.lastIndexOf('=') + 1);
window.alert(id);
return true;
}
else {
return false;
}
}
So the quick and dirty answer would be
var discount = window.location.search.split("?discount=")[1];
alert(discount);
But this doesn't take into account the occurence of other query string parameters.
You'll really want to parse all the query parameters into a hash map.
This article does a good job of showing you a native and jQuery version.
http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

Custom pages number (roman numbering) on pdf first pages

I need to set page numbers on a pdf I'm creating, so that the first 3 pages would be i,ii,iii and then the following pages starting from 1,2,3,4,5...and so on..
How can i do it with itextsharp??
Thanks
Sander
Check out the example in Massoud Mazar's blog. Look at his override for the OnEndPage event in the TwoColumnHeaderFooter class and see how he prints out page numbers.
What you can do is examine the event's PdfWriter parameter's PageNumber property and custom set the string you'll use for the displayed page number.
Something like this:
String text = "";
int pageN = writer.PageNumber;
if (pageN == 1) {
text = "i";
} else if (pageN == 2) {
text = "ii";
} else if (pageN == 3) {
text = "iii";
} else {
text = (pageN - 3).ToString();
}
Would replace his original:
String text = "Page " + pageN + " of ";

how to check each elements of string array contains data or not in c#

i have created web application and using textbox and it can contains multiple line of data becoz i have set its textmode property is multiline.
my problem is that i want to check each line contain data or not so i using count variable which count how many line contain data.
string[] data;
int cntindex;
data = txt_invoicenumber.Text.ToString().Split("\n".ToCharArray());
cntindex = data.Length;
for (j = 0; j < cntindex; j++)
{
if (data[j]!="")
{
inv_count++;
}
}
Its not working.
Please help me.
I guess this is because new line is \r\n so there is a '\r' also on empty lines.
Change the if statement to:
if (data[j].Trim().Length != 0)
Firstly, You don't need to ToString() the .Text property as it is already a string.
try this
string[] lines = txt_invoicenumber.Text.Split(Environment.NewLine);
int lineCount = 0;
foreach(string line in lines)
{
if(!string.IsNullOrEmpty(line))
{
lineCount ++;
this.ProcessLine(line);
}
}
var lb = new String[] { "\r\n" };
var lines = txt_invoicenumber.Text.Split(lb, StringSplitOptions.None).Length;
This will count empty lines too. If you don't want to count empty lines, use the StringSplitOptions.RemoveEmptyEntries value.
Don't count 100% on "\r\n" if you have little control over your environment though.
This is the answer I came up with.
String[] lines = TextBox1.Text.Split(new Char[] { '\r', '\n' },
StringSplitOptions.RemoveEmptyEntries);
Int32 validLineCount = lines.Length;