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);
}
Related
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.
Lets Say this is My Text. Now I want to Extract All 4 Variable Separately from the text
"ScanCode=? scanMsg= ? ItemName=? ID= ?\n"
Please Help i need this is Dart, Flutter
The solution I developed first splits the data according to the space character. It then uses the GetValue() method to sequentially read the data from each piece. The next step will be to use the data by transforming it accordingly.
This example prints the following output to the console:
[ScanCode=1234, ScanMessage=Test, Itemname=First, ID=1]
[1234, Test, First, 1]
The solution I developed is available below:
void main()
{
String text = "ScanCode=1234 ScanMessage=Test ItemName=First ID=1";
List<String> original = text.split(' ');
List<String> result = [];
GetValue(original, result);
print(original);
print(result);
}
void GetValue(List<String> original, List<String> result)
{
for(int i = 0 ; i < original.length ; ++i)
{
result.insert(i, original[i].split('=')[1]);
}
}
I have number of Strings coming from an API.
What I want is to merge all Strings together...
What I've done so far is store all Strings in an Array and convert that to a String:
var a = List<String>();
a.add("\n \u2022 " + "test1");
a.add("\n \u2022 " + "test2");
Result:
[•test1
•test2
]
Expected:
bulleted lists without [] .
Is there a better way to do this?
This code sample should answer your questions:
void main() {
const itemPrefix = " \u2022 ";
// create a growable list of strings
final strings = <String>[];
// add some items to it
strings.add("test1");
strings.add("test2");
// create a single string joining the items
String result = strings
// prepend the bullet point to each item
.map((item) => "${itemPrefix}$item")
// put a new-line between each item, joining the items to a String
.join('\n');
print(result);
}
I am reading a text file consisting of several lines, each line contains two numbers, one of them expresses the user name and the other expresses the password, the text appears as follows:
7829-613
2076-386
3001-007
5916-477
9782-858
3928-345
3574-189
I changed - to : by this code:
text= text.replaceAll('-', ':');
The result is :
7829:613
2076:386
3001:007
5916:477
9782:858
3928:345
3574:189
I tried to replace \n to , by this code:
text= text.replaceAll('-', ':')..replaceAll('\n',',');
So that I can separate each line, key and value, but not work
Is there any way to get Mapfrom this text
Thanks advance
Try this
var text = "7829-613\n2076-386\n3001-007\n5916-477\n9782-858\n3928-345\n3574-189";
text = text.replaceAllMapped(RegExp(r"[0-9]{3,4}"), (match) => "\"${match.group(0)}\"");
text = text.replaceAll("-", ":");
text = text.replaceAll("\n", ",");
var map = jsonDecode("{$text}");
print(map);
Just use a r before to the String to read raw special characters.
String text = r'mmmmm\nssdsds\n';
text = text.replaceAll(r'\n', ',');
Before you open your file, you can declare final credentials = Map<String, dynamic>.
Then, you can iterate through each line of the file with an input stream as in https://api.dart.dev/stable/2.9.2/dart-io/File-class.html.
That is the following, where in each line you split the string by the colon(:) separator and assign those key value pairs to the map:
import 'dart:io';
import 'dart:convert';
import 'dart:async';
main() {
final credentials = Map<String, dynamic>
final file = new File('file.txt');
Stream<List<int>> inputStream = file.openRead();
inputStream
.transform(utf8.decoder) // Decode bytes to UTF-8.
.transform(new LineSplitter()) // Convert stream to individual lines.
.listen((String line) { // Process results.
final decodedLine = line.split(":");
credentials[decodedLine[0]] = credentials[decodedLine[1]];
},
onDone: () { print('File is now closed.'); },
onError: (e) { print(e.toString()); });
}
Please let me know if you have any questions about this. I tried to be as thorough as I could :)