How to split a string in Flutter while also including delimiters - flutter

+37.4054-122.0999/
The above coordinates are my output. I want to split the string in such a way that it shows +37.4054 and -122.0999 as substrings which includes the + and - signs.

You can do
string.split(RegExp('(?=[+-])'));
Example:
var string = '+37.4054-122.0999';
var string2 = '-37.4054+122.0999';
var string3 = '+37.4054+122.0999';
var string4 = '-37.4054-122.0999';
var a = string.split(RegExp('(?=[+-])'));
var b = string2.split(RegExp('(?=[+-])'));
var c = string3.split(RegExp('(?=[+-])'));
var d = string4.split(RegExp('(?=[+-])'));
print(a);
print(b);
print(c);
print(d);
Output:
[+37.4054, -122.0999]
[-37.4054, +122.0999]
[+37.4054, +122.0999]
[-37.4054, -122.0999]

Try use split method:
const string = '+37.4054-122.0999';
final splitted = string.split('-');
print(splitted); //['+37.4054', '122.0999'];
print(splitted[0]); //+37.4054;
print('-' + splitted[1]); //-122.0999;

Try this:
void main() {
final String coord = '+37.4054-122.0999';
final int plusIndex = coord.indexOf('+');
final int minusIndex = coord.indexOf('-');
final plus = coord.substring(plusIndex, minusIndex);
final minus = coord.substring(minusIndex, );
print('plus: $plus, minus: $minus');
}

Related

How to extract specific string from whole string?

I have following strings :
String? hello = "(1.2,1.5 | 5)"
String? hi = "(2.3,3.2 | 9)"
Now I want to get
var newhello1 = 1.2,1.5
var newhello2 = 5
and
var newhi1 = 2.3,3.2
var newhi2 = 9
How to extract those text from that entire strings?
You can use the indexOf function combined with the substring to get the substrings as follows
var newhello1 = hello.substring(hello.indexOf('(') + 1, hello.indexOf('|')).trim(); //Use Trim() to get rid of any extra spaces
var newhello2 = hello.substring(hello.indexOf('|') + 1,hello.indexOf(')')).trim();
print(newhello1); //1.2,1.5
print(newhello2); //5
List<String> myformatter(String? data) {
if (data == null) return [];
List<String> ls = data.split("|");
for (int i = 0; i < ls.length; i++) {
ls[i] = ls[i].replaceAll("(", "").replaceAll(")", "").trim();
}
return ls;
}
main() {
String? hello = "(1.2,1.5 | 5)";
String? hi = "(2.3,3.2 | 9)";
final helloX = myformatter(hello);
print(helloX[0]); //1.2,1.5
print(helloX[1]); //5
final hiX = myformatter(hi);
print(hiX[0]); //2.3,3.2
print(hiX[1]); //9
}

Flutter search 'keywords' inside a String?

I want to search for a particular keyword in a String. For example, there is a string 'rajasthan' now if I want to search using 'rj', it will not detect anything, but if I search using 'raj' then it will. So what to do in this case?
My current way of searching:
String raj = 'rajasthan';
bool searchRaj = raj.contains('raj');
bool searchRJ = raj.contains('rj');
print('raj contains $searchRaj');
print('rj contains $searchRJ');
Dartpad:
https://dartpad.dev/?id=40472ce8cd82bd87ba916ea2f3e7eff9
Here you can find the example of how to achieve this.
void main() {
String raj = 'rajasthan';
String search1 = 'raj';
String search2 = 'rj';
var searchList = raj.split("").toSet();
var searchRAJ = search1.split("").toSet();
var searchRJ = search2.split("").toSet();
bool result1 = searchList.containsAll(searchRAJ);
bool result2 = searchList.containsAll(searchRJ);
print('raj contains $result1');
print('rj contains $result2');
}

Flutter convert string in list double

I have a response from REST API that return this:
var time = [{"duration":"00m 25s"},{"duration":"12m 08s"},{"duration":"02m 09s"},{"duration":"01m 25s"}, {"duration":"02m 05s"}]
I want to transform this list in:
var newTime = [0.25, 12.08, 2.09, 1.25, 2.05]
You can do string manipulation using splitting string using some delimiter like space and applying transformation via map.
void main() {
var time = [
{"duration": "00m 25s"},
{"duration": "12m 08s"},
{"duration": "02m 09s"},
{"duration": "01m 25s"},
{"duration": "02m 05s"}
];
time.map((e) {
final val = e['duration'].split(' '); // split by space
final result = val[0].substring(0, val[0].length - 1) + '.' +
val[1].substring(0, val[1].length - 1); // concat number by removing unit suffix
return double.tryParse(result); // parsing to double.
}).forEach((e) => print(e)); // 0.25, 12.08, 2.09, 1.25, 2.05
}
You can do it as follows:
var time = [{"duration":"00m 25s"},{"duration":"12m 08s"},{"duration":"02m 09s"},{"duration":"01m 25s"}, {"duration":"02m 05s"}];
var newList = time.map((time) {
String clippedMinutes; // will get the minutes part
String clippedSeconds; //// will get the seconds part
String fullTime = time['duration']; // full time part from each Map
final splittedTimeList = fullTime.split(' '); // splits the full time
clippedMinutes = splittedTimeList[0];
clippedSeconds = splittedTimeList[1];
return double.parse('${clippedMinutes.substring(0, clippedMinutes.length - 1)}.${clippedSeconds.substring(0, clippedSeconds.length - 1)}');
}).toList();
print(newList); // output: [0.25, 12.08, 2.09, 1.25, 2.05]
If it helped you don't forget to upvote
My contribution:
main(List<String> args) {
final times = [{"duration":"00m 25s"},{"duration":"12m 08s"},{"duration":"02m 09s"},{"duration":"01m 25s"}, {"duration":"02m 05s"}];
var regExp = RegExp(r'(\d\d)m (\d\d)s');
var newData = times.map((e) => double.parse(e['duration'].replaceAllMapped(regExp, (m) => '${m[1]}.${m[2]}')));
print(newData);
}
Result:
(0.25, 12.08, 2.09, 1.25, 2.05)

How to format to json with difference between comma of data/key and comma from text?

I am trying to improve this code so that it can handle a specific case.
Currently it works, unless the user adds a text with a comma
Here is my input who work (look only "note" key/value)
Input_OK = 2020-11-25,note:my text,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
In this case : my text is ok because there is no comma
Input_NOK = 2020-11-25,note:my text, doesn't work,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
In this case : my text, doesn't work is not ok because there is comma
With this specific input 2020-11-25,note:my text, work now,2020-11-25,today:2020-11-25,2020-09-14,start:2020-09-14
I try to have this output
[{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, work now"},{"release_date":"2020-09-14","start":"2020-09-14"}]
Here is my current code
// before this input I add string to a list<String> for each date like that [2020-11-25,note0:test, 2020-11-24,my text, with comma, 2020-11-15,today:2020-11-15, 2020-09-14,start:2020-09-14]
//After I remove space and [ ]
// myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable ${inputItarable} ");
//inputItarable [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14]
var i = inputItarable.iterator;
var tmp = {};
while (i.moveNext()) {
var key = i.current; i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}
var output1 = tmp.keys.map((key) {
var map = {}; map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2=json.encode(output1);
print("output2 $output2 ");
// output2 [{"release_date":"2020-11-25","today":"2020-11-25","note0":"my text, with comma"},{"release_date":"2020-09-14","start":"2020-09-14"}]
[Edit] I have a spécific case, where user back ligne, and have an input like that
myinput 2020-11-25,today:2020-11-25,2020-11-25,note0:my text,
with comma,2020-09-14,start:2020-09-14
in this example I don't know how to replace the back ligne between my text, and with comma by my text,\nwith comma
Please check the code below or you may directly run it on Dartpad at https://dartpad.dev/1404509cc0b427b1f31705448b5edba3
I have written a sanitize function. What the sanitize function does is it sanitizes the text between the possibleStart and possibleEnd. Meaning it replaces all the commas in user input text with §. To do this it assumes that the user input starts with ,note: or ,note0: and ends with ,2020- or ,2021-. This sanitized string is passed to your code and in the end § is replaced with ",". Let me know if you have any questions.
import 'dart:convert';
String sanitize(
String input, List<String> possibleStart, List<String> possibleEnd) {
final String start = possibleStart.join("|");
final String end = possibleEnd.join("|");
final RegExp exp = RegExp("(?<=$start)(.*?)(?=$end)");
final Iterable<Match> matches = exp.allMatches(input);
matches.forEach((match) {
input =
input.replaceFirst(match.group(0), match.group(0).replaceAll(",", "§"));
return true;
});
return input;
}
void main() {
String myinput =
"2020-11-25,today:2020-11-25,2020-11-25,note0:my text, with comma,2020-09-14,start:2020-09-14";
myinput = sanitize(myinput, [",note:", "note\\d:"], [",20\\d\\d-"]);
var inputItarable = myinput.toString().split(',').where((s) => s.isNotEmpty);
print("inputItarable ${inputItarable} ");
//inputItarable [2020-11-25, today:2020-11-25, 2020-11-25, note0:my text, with comma, 2020-09-14, start:2020-09-14]
var i = inputItarable.iterator;
var tmp = {};
while (i.moveNext()) {
var key = i.current;
i.moveNext();
var value = i.current.split(':');
(tmp[key] ??= []).add(value);
}
var output1 = tmp.keys.map((key) {
var map = {};
map['release_date'] = key;
tmp[key].forEach((e) => map[e[0]] = e[1]);
return map;
}).toList();
var output2 = json.encode(output1).replaceAll("§", ",");
print("output2 $output2 ");
}

Android studio Lost connection to device

I have an issue where i am running this code and getting all different combinations of the number without repeating.
It is put in a for loop where I have a list of numbers.
If the list is only of just 1 number, it seems to be alright. However when I have multiple numbers in the list, Android studio loses connection to my device.
Is it because my app is doing too much? If not how do I fix it?
List<String> rollNumberGenerator(String num) {
List numberToBeRolled = num.split('');
List<String> generatedRollList = [];
String zero = numberToBeRolled[0];
String one = numberToBeRolled[1];
String two = numberToBeRolled[2];
String three = numberToBeRolled[3];
String rollNumber1 = '$zero$one$two$three';
String rollNumber2 = '$zero$one$three$two';
String rollNumber3 = '$zero$three$one$two';
String rollNumber4 = '$three$zero$one$two';
String rollNumber5 = '$three$zero$two$one';
String rollNumber6 = '$zero$three$two$one';
String rollNumber7 = '$zero$two$three$one';
String rollNumber8 = '$zero$two$one$three';
String rollNumber9 = '$two$zero$one$three';
String rollNumber10 = '$two$zero$three$one';
String rollNumber11 = '$two$three$zero$one';
String rollNumber12 = '$three$two$zero$one';
String rollNumber13 = '$three$two$one$zero';
String rollNumber14 = '$two$three$one$zero';
String rollNumber15 = '$two$one$three$zero';
String rollNumber16 = '$two$one$zero$three';
String rollNumber17 = '$one$two$zero$three';
String rollNumber18 = '$one$two$three$zero';
String rollNumber19 = '$one$three$two$zero';
String rollNumber20 = '$three$one$two$zero';
String rollNumber21 = '$three$one$zero$two';
String rollNumber22 = '$one$three$zero$two';
String rollNumber23 = '$one$zero$three$two';
String rollNumber24 = '$one$zero$two$three';
generatedRollList.add(rollNumber1);
generatedRollList.add(rollNumber2);
generatedRollList.add(rollNumber3);
generatedRollList.add(rollNumber4);
generatedRollList.add(rollNumber5);
generatedRollList.add(rollNumber6);
generatedRollList.add(rollNumber7);
generatedRollList.add(rollNumber8);
generatedRollList.add(rollNumber9);
generatedRollList.add(rollNumber10);
generatedRollList.add(rollNumber11);
generatedRollList.add(rollNumber12);
generatedRollList.add(rollNumber13);
generatedRollList.add(rollNumber14);
generatedRollList.add(rollNumber15);
generatedRollList.add(rollNumber16);
generatedRollList.add(rollNumber17);
generatedRollList.add(rollNumber18);
generatedRollList.add(rollNumber19);
generatedRollList.add(rollNumber20);
generatedRollList.add(rollNumber21);
generatedRollList.add(rollNumber22);
generatedRollList.add(rollNumber23);
generatedRollList.add(rollNumber24);
List<String> validGeneratedRollList = [];
for (var numbers in generatedRollList) {
bool present = false;
present = validGeneratedRollList.contains(numbers);
if (present == false) {
validGeneratedRollList.add(numbers);
}
}
return validGeneratedRollList;
}
Thanks in advance for anyone that can help!