How to extract specific string from whole string? - flutter

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
}

Related

How to split a string in Flutter while also including delimiters

+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');
}

split the string into equal parts flutter

There is a string with random numbers and letters. I need to divide this string into 5 parts. And get List. How to do it? Thanks.
String str = '05b37ffe4973959c4d4f2d5ca0c1435749f8cc66';
Should work:
List<String> list = [
'05b37ffe',
'4973959c',
'4d4f2d5c',
'a0c14357',
'49f8cc66',
];
I know there'a already a working answer but I had already started this so here's a different solution.
String str = '05b37ffe4973959c4d4f2d5ca0c1435749f8cc66';
List<String> list = [];
final divisionIndex = str.length ~/ 5;
for (int i = 0; i < str.length; i++) {
if (i % divisionIndex == 0) {
final tempString = str.substring(i, i + divisionIndex);
list.add(tempString);
}
}
log(list.toString()); // [05b37ffe, 4973959c, 4d4f2d5c, a0c14357, 49f8cc66]
String str = '05b37ffe4973959c4d4f2d5ca0c1435749f8cc66';
int d=1
; try{
d = (str.length/5).toInt();
print(d);
}catch(e){
d=1;
}
List datas=[];
for(int i=0;i<d;i++){
var c=i+1;
try {
datas.add(str.substring(i * d, d*c));
} catch (e) {
print(e);
}
}
print(datas);
}
OR
String str = '05b37ffe4973959c4d4f2d5ca0c1435749f8cc66';
int d = (str.length / 5).toInt();
var data = List.generate(d - 3, (i) => (d * (i + 1)) <= str.length ? str.substring(i * d, d * (i + 1)) : "");
print(data);//[05b37ffe, 4973959c, 4d4f2d5c, a0c14357, 49f8cc66]
If you're into one liners, with dynamic parts.
Make sure to import dart:math for min function.
This is modular, i.e. you can pass whichever number of parts you want (default 5). If you string is 3 char long, and you want 5 parts, then it'll return 3 parts with 1 char in each.
List<String> splitIntoEqualParts(String str, [int parts = 5]) {
int _parts = min(str.length, parts);
int _sublength = (str.length / _parts).ceil();
return Iterable<int>
//Initialize empty list
.generate(_parts)
.toList()
// Apply the access logic
.map((index) => str.substring(_sublength * index, min(_sublength * index + _sublength, str.length)))
.toList();
}
You can then use it such as print(splitIntoEqualParts('05b37ffe4973959c4d4f2d5ca0c1435749f8cc66', 5));
splitWithCount(String string,int splitCount)
{
var array = [];
for(var i =0 ;i<=(string.length-splitCount);i+=splitCount)
{
var start = i;
var temp = string.substring(start,start+splitCount);
array.add(temp);
}
print(array);
}

how to replace array object in flutter

if(_list.length != 0){
var shape;
var shapeCount;
var shapeCaratCount;
var shapeTotalAmount;
var details = new Map();
var temp = [Map()];
for(int l = 0 ; l <= _list.length; l++){
var list = _list[l];
if(temp.contains(list.shape)){
shapeCount += 1;
shapeCaratCount = shapeCaratCount + list.carat;
shapeTotalAmount = shapeTotalAmount + list.totalAmount;
details['Shape'] = list.shape;
details['pcs_count'] = shapeCount;
details['Carat_Count'] = shapeCaratCount;
details['total_amount'] = shapeTotalAmount;
print(details);
temp.add(details);
}else{
shapeCount = 0;
//shapeCaratCount = shapeCaratCount + list.carat;
shapeCaratCount = list.carat;
shapeTotalAmount = list.totalAmount;
details['Shape'] = list.shape;
details['pcs_count'] = shapeCount;
details['Carat_Count'] = shapeCaratCount.toString();
details['total_amount'] = shapeTotalAmount.toString();
print(details);
temp.add(details);
}
}
}
I want to replace array object of temp dictionary array.
so if any know how to do this please help i am new at flutter
this is my code please if any difficulty. please ask.

Is there a way to replace multiple keywords in a string and wrapping them with their own keyword?

Say I have a string:
typed = "need replace this ap"
str = "hello I need to replace this asap"
so the end result I want would be this:
newStr = "hello I <bold>need</bold> to <bold>replace</bold> <bold>this</bold> as<bold>ap</bold>"
please don't mind the weird syntax.
I wonder if the order would matter, for example:
typed = "applicable app"
str = "the app is very applicable in many applications"
The end result I wish should be:
newStr = "the <bold>app</bold> is very <bold>applicable</bold> in many <bold>app</bold>lications"
right? is this possible?
Hey,If You can ignore the weird HTML syntax here,
Then I have wrote a solution for you,
Paste this code in dart pad here
removeDuplicates(var typed, var str) {
Map<String, String> m = new Map<String, String>();
var n = typed.length;
String ans = "";
//for storing the "typed" string (word by word) into a map "m" variable for later searching purpose
String temp = "";
int i = 0;
for (i = 0; i < n; i++) {
if (typed[i] == " ") {
m[temp] = temp;
temp = "";
} else {
temp = temp + typed[i];
}
}
//for storing the last word of the string "typed", coz loop will never find a space in last of the string
m[temp] = temp;
// map variable loop for search from map "m" in the "str" string, and matching if the word is present or not
var n2 = str.length;
String temp2 = "";
for (int j = 0; j < n2; j++) {
if (str[j] == " ") {
if (m.containsKey(temp2)) {
} else {
ans = ans + " " + temp2; //storing the "temp2" string into "ans" string, everytime it finds a space and if the string is not already present in the map "m"
}
temp2 = "";
} else {
temp2 = temp2 + str[j];
}
}
//for searching for the last word of the string "str" in map "m", coz loop will never find a space in last of the string,
if (m.containsKey(temp2)) {
} else {
ans = ans + " " + temp2;
}
return ans;
}
void main() {
String typed = "need replace this ap";
var str = "hello I need to replace this asap";
String answer = removeDuplicates(typed, str);
print(answer);
}
Here, I have made a method removeDuplicates() to simplify your work, You just have to pass those string in your method, and then it will return you the desired answer string by removing the duplicates, with a new string.
UPDATED CODE (TO SHOW HTML CODE):
removeDuplicates(var typed, var str) {
Map<String, String> m = new Map<String, String>();
var n = typed.length;
String ans = "";
//for storing the "typed" string (word by word) into a map "m" variable for later searching purpose
String temp = "";
int i = 0;
for (i = 0; i < n; i++) {
if (typed[i] == " ") {
m[temp] = temp;
temp = "";
} else {
temp = temp + typed[i];
}
}
//for storing the last word of the string "typed", coz loop will never find a space in last of the string
m[temp] = temp;
print(m);
// map variable loop for search from map "m" in the "str" string, and matching if the word is present or not
var n2 = str.length;
String temp2 = "";
for (int j = 0; j < n2; j++) {
if (str[j] == " ") {
if (m.containsKey(temp2)) {
temp2 = "<bold>" + temp2 + "</bold> ";
ans = ans + " " + temp2;
} else {
ans = ans +
" " +
temp2; //storing the "temp2" string into "ans" string, everytime it finds a space and if the string is not already present in the map "m"
}
temp2 = "";
} else {
temp2 = temp2 + str[j];
}
}
//for searching for the last word of the string "str" in map "m", coz loop will never find a space in last of the string,
if (m.containsKey(temp2)) {
temp2 = "<bold>" + temp2 + "</bold> ";
temp2 = "";
} else {
ans = ans + " " + temp2;
temp2 = "";
}
return ans;
}
void main() {
var typed = "applicable app";
var str = "the app is very applicable in many applications";
String answer = removeDuplicates(typed, str);
print(answer);
}
UPDATE 2 (ALL THANKS TO PSKINK FOR THE str.replaceAllMapped APPROACH)
replaceWithBoldIfExists(String typed, String str) {
var n = typed.length;
List<String> searchList = new List<String>();
String temp = "";
int i = 0;
for (i = 0; i < n; i++) {
if (typed[i] == " ") {
searchList.add(temp);
temp = "";
} else {
temp = temp + typed[i];
}
}
searchList.add(temp);
String pat = searchList.join('|');
final pattern = RegExp(pat);
final replaced =
str.replaceAllMapped(pattern, (m) => '<bold>${m.group(0)}</bold>');
return replaced;
}
void main() {
var typed = "need replace this ap";
var str = "hello I need to replace this asap";
print(replaceWithBoldIfExists(typed, str));
}

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!