Lets say that I have a string:
a="23questions";
b="2questions3";
Now I need to parse 23 from both string. How do I extract that number or any number from a string value?
The following code can extract the number:
aStr = a.replaceAll(new RegExp(r'[^0-9]'),''); // '23'
You can parse it into integer using:
aInt = int.parse(aStr);
const text = "23questions";
Step 1: Find matches using regex:
final intInStr = RegExp(r'\d+');
Step 2: Do whatever you want with the result:
void main() {
print(intInStr.allMatches(text).map((m) => m.group(0)));
}
Related
Looking for the same solution that was given in swift here - How to remove first word from a sentence in swift.
Anyone can help?
void main() {
String words = "hello world everyone";
List<String> word_l = words.split(" ");
String word = word_l.sublist(1,word_l.length).join(" ");
print(word);
}
Use as above code to remove first word from words. This work for multiple words more than 2.
You could just do this:
void main() {
var data = 'CITY Singapore';
data = data[0];
print(data);
}
for example if I have a string = "I Like To Play Football" and a list = [Car,Ball,Door,Sky] it should give true.
Use any of list
var list = ["Car","Ball","Door","Sky"];
String text = "i like to play football";
if (list.any((item) => text.toLowerCase().contains(item))) {
//Text has a value from list
}
Here You Go:
void main() {
final String tempString = "I Like To Play Football";
final List<String> tempList = ["Car","Ball","Door","Sky"];
for(var i=0; i < tempList.length; i ++) {
if(tempString.contains(tempList.elementAt(i).toLowerCase())){
print("Found and its ${tempList[i]}");
}
}
}
Regex is your friend here. You can make a simple regex that uses each string in the array as an option (and make it case insensitive) then run the match. I've made an example here in javascript, but it's easy to do in dart
https://api.dart.dev/stable/2.16.1/dart-core/RegExp-class.html
const source = "I Like To Play Football";
const toMatch = ["Car","Ball","Door","Sky"];
let regexString = '';
for (const option of toMatch) {
//adding | modifier after string. Last one is redundant of course
//also I'm not checking for special regex characters in toMatch, but that might be necessary.
regexString += option + '|';
}
// using slice to remove last |
console.log(regexString.slice(0, -1));
const regexp = new RegExp(regexString.slice(0, -1), 'i');
console.log(source.match(regexp));
Here's a short version:
var src = 'I Like To Play Football'.split(' ');
var list = ['Car','Ball','Door','Sky'];
var result = list.any((x) => src.any((y) => y.toLowerCase().contains(x.toLowerCase())));
print(result);
I have a list of numbers like below -
List contacts = [14169877890, 17781231234, 14161231234];
Now I want to find if one of the above list element would contain the below string value -
String value = '4169877890';
I have used list.any to do the search, but the below print statement inside the if condition is not printing anything.
if (contacts.any((e) => e.contains(value))) {
print(contacts[0]);
}
I am expecting it to print out the first element of the contacts list as it partially contains the string value.
What is it I am doing wrong here?
contacts isn't a List<String>, so your any search can't be true, you need turn element of contracts to string to able to use contains.
void main() {
var contacts = [14169877890, 17781231234, 14161231234];
print(contacts.runtimeType);
var value = '4169877890';
print(value.runtimeType);
var haveAnyValid = contacts.any((element) {
return "$element".contains(value);
});
print(haveAnyValid);
// result
// JSArray<int>
// String
// true
}
Not sure if contacts is an integer and value is a string on purpose or mistake, but this works in dart pad if you convert it to string:
if (contacts.any((e) => e.toString().contains(value))) {
print(contacts[0]);
}
DartPad Link.
I want to enter a array like [1,2,3,4,5] in the form field and use that form value as a parameter to bubblesort function to sort the array.How to implement this in flutter?
Keep it simple and just use the builtin json parser.
Like this:
List<int> nbs = List<int>.from(json.decode('[1,2,3,4]'));
Our string:
final hi = "[1,2,3,4,5]";
The regular expression used to remove the square brackets:
final regex = RegExp(r'([\[\]])');
Our string without any brackets after replacing them with nothing:
final justNumbers = hi.replaceAll(regex, ''); // 1,2,3,4,5
Our list of strings by splitting them by commas:
List<String> strings = justNumbers.split(',');
Now we parse our strings into integers (tryParse is used so that it returns null instead of throwing an exception):
List<int> numbers = strings.map((e) => int.tryParse(e)).toList();
Final
void main() {
final hi = "[1,2,3,4,5]";
final regex = RegExp(r'([\[\]])');
final justNumbers = hi.replaceAll(regex, '');
List<String> strings = justNumbers.split(',');
List<int> numbers = strings.map((e) => int.tryParse(e)).toList();
print(numbers);
}
I have a string and say I want to check the last 3 digits of the string for some range.
if the string is like sasdaX01, I need to check the last three digits of the string are between X01-X50.
ANy help would be highly appreciated.
The use spl.string::*; line is mandatory and then you extract your last digits with substring(info, length(info) - 3, 3).
An example:
use spl.string::*;
composite TestComposite {
graph
// ... code generating (stream<rstring info> testStream)
() as PrintTestInfo = Custom(testStream as infoEvents) {
logic
onTuple infoEvents : {
rstring lastDigits = substring(info, length(info) - 3, 3);
boolean matched = (lastDigits >= "X01" && lastDigits <= "X50");
println(info + " matched > " + (rstring)matched) ;
} // onTuple infoEvents
} // PrintTestInfo
} // TestComposite