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');
}
Related
+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');
}
As you can see I have a list:
List avatarList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
AssetsResources.VIP5,
AssetsResources.w1,
AssetsResources.w2,
];
I understand I can use method:
final bool isVIP = avatarList[index].contains('VIP');
But since AssetsResources.VIP1 is not a String like 'VIP1'but a path from other dart file, so here I have no idea how to check if the element from avatarList contains VIP value, thanks for any clue!
Update
Thanks guys for the help and sorry I didnt describe clearly, what I mean is, if
List idealList = [
'vip1',
'vip2',
'vip3',
'vip4',
'vip5',
];
so the elements in the idealList is 'vip1' but in my case the list myList is
List myList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
AssetsResources.VIP5,
AssetsResources.w1,
AssetsResources.w2,
];
So it seems I can not directly use some methode as follows
final bool isVIP = myList[index].contains('VIP');
since the elements from myList is just a path(sorry I dont know how to call this value), could you please let me know in my case how to check if this path contains 'VIP' value? thanks!
Update
yes, AssetsResources is very simple, just store the asset path:
class AssetsResources {
/*worm avatar*/
static const String VIP1 = 'assets/worms/VIP_1.svg';
static const String VIP2 = 'assets/worms/VIP_2.svg';
static const String VIP3 = 'assets/worms/VIP_3.svg';
static const String VIP4 = 'assets/worms/VIP_4.svg';
}
The code should work fine :
class AssetsResources {
/*worm avatar*/
static const String VIP1 = 'assets/worms/VIP_1.svg';
static const String VIP2 = 'assets/worms/VIP_2.svg';
static const String VIP3 = 'assets/worms/VIP_3.svg';
static const String VIP4 = 'assets/worms/VIP_4.svg';
}
void main() {
List myList = [
AssetsResources.VIP1,
AssetsResources.VIP2,
AssetsResources.VIP3,
AssetsResources.VIP4,
];
for (final asset in myList) {
print(asset);
print(asset.contains('VIP'));
}
}
The above prints :
assets/worms/VIP_1.svg
true
assets/worms/VIP_2.svg
true
assets/worms/VIP_3.svg
true
assets/worms/VIP_4.svg
true
If I understood you correctly.
void main() {
for(var i = 0; i < avatarList.length; i++) {
String element = avatarList[i];
if(element.contains('VIP')) {
print(other.contains(element)); // true
print(other.firstWhere((e) => e.contains(element))); // 'VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5'
}
}
}
List<String> avatarList = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
List<String> other = ['VIP1', 'VIP2', 'VIP3', 'VIP4', 'VIP5', 'w1', 'w2'];
I want to check if the value that the user enters in a TextFormField contains at least one String in another given String.
As an example, if the given String value is 0123456789 and if the user enters Ab3 in the TextFormField, how to check if the user entered value contains at least one String in the given String?
String allowedChar = "0123456789";
final split = allowedChar.split('');
I tried splitting the given value like this and checked if the textEditingController.text contains the value for each splitted value of allowedChar.
if (_value.isNotEmpty &&
textEditingController.text.contains(c)) {
_areAlwdCharsTyped = true;
} else if (!textEditingController.text.contains(c)) {
_areAlwdCharsTyped = false;
}
But _areAlwdCharsTyped always returns false. Could I know a way to achieve this please? Thanks in advance.
void main() {
const text = 'Ab3';
var match = RegExp(r'\d').hasMatch(text);
print(match);
}
Result
true
I think you're close. You can iteratively check whether the input string contains any of the chars in allowedChars.
void main() {
String allowedChar = "0123456789";
final split = allowedChar.split('');
final str = 'AB3';
final str2 = 'AB';
print(split.fold<bool>(false, (prev, element) => str.contains(element) || prev)); // true
print(split.fold<bool>(false, (prev, element) => str2.contains(element) || prev)); // false
}
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 ");
}
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!