How to use a String in a List dart? - dart

How to turn a String into a List in Dart? I'm sharing some code to let you know what I want to achieve.
String testString = "'banana', 'apple', 'peach'";
List testList = [testString];
print(testList); // This is where I want to get 'banana' instead I get the whole String back.
Thanks in advance

I think split method is what you need.
it will split the string at matches of pattern and returns a list of substrings.
String testString = "banana,apple,peach";
List testList = testString.split(',');
print(testList);
check out this link for more info.

Related

How to convert string into list string on specific expression in Dart?

i have string who is
String hello = "test test {Brian} have you {Adam} always and {Corner} always";
want to make list string who is taking who have {string}
output :
List<String> data = ["{Brian}","{Adam}","{Corner}"];
its that possible in dart ?
i dont know what to use
You can achieve this by using RegExp(r"\{[^{}]*\}")
String hello = "test test {Brian} have you {Adam} always and {Corner} always";
RegExp regExp = RegExp(r"\{[^{}]*\}");
print(regExp.allMatches(hello).map((e) => e[0]).toList());

Processing json string in mql4

I have received the following string:
{"records":[{"id":"rec4haaOncoQniu8U","fields":{"orders1":5},"createdTime":"2020-02-08T09:08:22.000Z"}]}
I am not understanding how I can process and separate the values of the json in mql4 using the "JAson.mqh " library, located here: https://www.mql5.com/en/code/13663
I need the values of "orders" located under "fields" , value = 5.
the only "KEYS" that changes are the keys within the "fields" values.
i would like to be able to get the values with something like this:
string value1 = Result[0].["fields"].["orders1"]; //5
string value2 = Result[0].["fields"].["orders2"];
Please let me know what I can do.
You can get the value using the following format. Note that it has to be casted to a type. (I have casted it to int as it is the type it is in the JSON, but you can cast it to string as well)
int value1 = json["records"][0]["fields"]["orders1"].ToInt(); // if you want to make it a string use ToStr() instead of ToInt()
Here is a full example of what I did
string jsonString = "{\"records\": [{\"id\": \"rec4haaOncoQniu8U\",\"fields\": {\"orders1\": 5 }\"createdTime\": \"2020-02-08T09:08:22.000Z\"}]}";
if(json.Deserialize(jsonString))
Alert(json["records"][0]["fields"]["orders1"].ToInt());
Hope it helped.

comparing two string lists and check if they have at least one same string

I'm trying to compare two String list with each other and check if at least the have one exact same string or not ..
For example:
List<String> list1 = ['1','2','3','4'];
List<String> list2 = ['1','5','6','7'];
In this case I will do action cause both have same string which is 1, and it could be more than one exact same string and the action will be the same.
But if they don't have any similar strings then I will do another action.
How can I do something like this?
You can do it with any() and contains() method:
if (list1.any((item) => list2.contains(item))) {
// Lists have at least one common element
} else {
// Lists DON'T have any common element
}
Set has an intersection that does that:
list1.toSet().intersection(list2.toSet()).length > 0
A shorter version:
bool hasCommonElement = list1.any(list2.contains);

How can I convert a string to a char array in ActionScript 3?

How do you convert a string into a char array in ActionScript 3.0?
I tried the below code but I get an error:
var temp:ByteArray = new ByteArray();
temp = input.toCharArray();
From the error, I understand that the toCharArray() function cannot be applied to a string (i.e in my case - input). Please help me out. I am a beginner.
I am not sure if this helps your purpose but you can use String#split():
If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array.
var array:Array = "split".split("");
Now you can get individual elements using index
array[0] == 's' ; array[1] == 'p' ....
Depending on what you need to do with it, the individual characters can also be accessed with string.charAt(index), without splitting them into an array.

Help With pattern matching

In my code I have to match below 3 types of data
abcd:xyz:def
def:xyz
xyz:def
where "xyz" is the real data and other part are Junk data. Now, for first 2 types as below I can split with ':' and can get the array[1] position data ... which will give me the correct one.
abcd:xyz:def
def:xyz
I am not getting How can extract the 3rd case. Any idea? Please help.
Thanks,
Rahul
string case1 = "abcd:xyz:def";
string case2 = "def:xyz";
string case3 = "xyz:def";
string result1 = case1.Split(':')[1];
string result2 = case2.Split(':')[1];
string result3 = case3.Split(':')[0];
If I understand your question correctly.
Use array[0] instead of array[1] in the third case after splitting.

Resources