How to extract double from string in dart? - dart

I have a string containing 3 or 4 double numbers. what's the best way to extract them in an array of numbers?

First you have to find the numerals. You can use a RegExp pattern for that, say:
var doubleRE = RegExp(r"-?(?:\d*\.)?\d+(?:[eE][+-]?\d+)?");
Then you parse the resulting strings with double.parse. Something like:
var numbers = doubleRE.allMatches(input).map((m) => double.parse(m[0])).toList();

Related

How can i use scan in Ruby to select specific symbols

i start code in Ruby, but i want to know if in scan i could selection some operations symbol.
For example:
var = ["+100,-20,-15,+30"]
var.scan(/\d+/).map{|i| print i.to_i}
and my answer is:
0312100100102001010010010010
but i need keep ",+-", this si possible with scan?
because my problem is get a finish result in var
var in your question is an array, not a string (the square brackets on each side indicates it is an array). But if you did have a comma-separated string of integers and wanted to add those integers, like your comment suggested, you could do this:
var = "+100,-20,-15,+30"
var.split(",").map(&:to_i).sum(0)
# => 95

How to specify the number of decimal places in a double?

How does one specify the number of decimal places when outputting a string ?
For example, say I have the following:
var root3 = 1.73205080757;
And wish to output to two decimal places, how do I format the string, similar to how one does so in Java ?
doubles in dart have a method toStringAsFixed() which is easy to use:
If you have :
var root3 = 1.73205080757;
You can just do:
print(root3.toStringAsFixed(2));
Output:
1.73

Splitting string into parts with regex in Dart

If i have some test data john_doe01.jpg how can i use regex or something else to split this into separate strings like..
john_doe or john and doe
01
jpg
You create a RegExp using the constructor, like: var re = RegExp(r"\d+|[a-zA-Z]+");.
You can get the substrings matching the RegExp as: var matches = [...re.allMatches(myText)];
In your case, var myText = "john_doe01.jpg";, that would give you the result ['john", "doe", "01", "jpg"].
If that's not what you want, you'll have to fiddle with the RegExp until it gives you what you do want. It usually helps writing down in prose, and in excruciating detail, what it is you want, and then writing the RegExp afterwards.

How to convert string to raw string in dart

I want to convert an existing string to raw string.
like:
String s = "Hello \n World"
I want to convert this s variable to raw string(I want to print exact "Hello \n Wrold")
I need backslash(\) in output. I am trying to fetch string value from rest api. it have bunch of mathjax(latex) formula containing backslash.
Thanks
You are asking for a way to escape newlines (and possibly other control characters) in a string value.
There is no general way to do that for Dart strings in the platform libraries, but in most cases, using jsonEncode is an adequate substitute.
So, given your string containing a newline, you can convert it to a string containing \n (a backslash and an n) as var escapedString = jsonEncode(string);. The result is also wrapped in double-quotes because it really is a JSON string literal. If you don't want that, you can drop the first and last character: escapedString = escapedString.substring(1, escapedString.length - 1);.
Alternatively, if you only care about newlines, you can just replace them yourself:
var myString = string.replaceAll("\n", r"\n");

extract a letter from a string in Lua

i have the string price that has a value with a number in it. I have code that extracts the number, I need help to figure out how to have another string (pricechar) with only the "k" in it
price="1k"
--pricechar=...
pricenum=string.match(price,"%d+")
You can extract all non-numeric characters, similar to how you do it for numbers:
pricechar = string.match(price,"[^%d]+")
To get both values at the same time:
pricenum, pricechar = string.match(price,"(%d+)(.*)")

Resources